From e170eae3e0300917513fc9c9ce85f98e34d15293 Mon Sep 17 00:00:00 2001 From: pulkitvajpayee07 Date: Mon, 11 Apr 2022 10:49:03 +0530 Subject: [PATCH] Removed_legacy_service_chain_code Change-Id: Ie7bcf691a48bcaedb9f5a8413136592608eb3897 --- gbpservice/contrib/nfp/__init__.py | 0 .../nfp/config_orchestrator/__init__.py | 0 .../config_orchestrator/common/__init__.py | 0 .../nfp/config_orchestrator/common/common.py | 189 -- .../common/lbv2_constants.py | 20 - .../nfp/config_orchestrator/common/topics.py | 23 - .../config_orchestrator/handlers/__init__.py | 0 .../handlers/config/__init__.py | 0 .../handlers/config/firewall.py | 179 - .../handlers/config/loadbalancerv2.py | 523 --- .../handlers/config/vpn.py | 198 -- .../handlers/notification/__init__.py | 0 .../handlers/notification/handler.py | 235 -- .../config_orchestrator/modules/__init__.py | 45 - .../modules/config_orch.py | 86 - .../contrib/nfp/configurator/__init__.py | 0 .../advanced_controller/__init__.py | 0 .../advanced_controller/controller.py | 338 -- .../advanced_controller/controller_loader.py | 64 - .../nfp/configurator/agents/__init__.py | 0 .../nfp/configurator/agents/agent_base.py | 280 -- .../nfp/configurator/agents/firewall.py | 510 --- .../nfp/configurator/agents/generic_config.py | 546 --- .../configurator/agents/loadbalancer_v2.py | 1017 ------ .../nfp/configurator/agents/nfp_service.py | 255 -- .../contrib/nfp/configurator/agents/vpn.py | 461 --- .../nfp/configurator/drivers/__init__.py | 0 .../nfp/configurator/drivers/base/__init__.py | 0 .../configurator/drivers/base/base_driver.py | 167 - .../configurator/drivers/firewall/__init__.py | 0 .../drivers/firewall/vyos/__init__.py | 0 .../firewall/vyos/vyos_fw_constants.py | 18 - .../drivers/firewall/vyos/vyos_fw_driver.py | 695 ---- .../drivers/loadbalancer/__init__.py | 0 .../drivers/loadbalancer/v2/__init__.py | 0 .../v2/common/neutron_lbaas_data_models.py | 728 ---- .../drivers/loadbalancer/v2/haproxy/config.py | 130 - .../loadbalancer/v2/haproxy/haproxy_driver.py | 699 ---- .../v2/haproxy/haproxy_driver_constants.py | 15 - .../v2/haproxy/local_cert_manager.py | 171 - .../v2/haproxy/rest_api_driver.py | 103 - .../drivers/nfp_service/__init__.py | 0 .../drivers/nfp_service/heat/__init__.py | 0 .../drivers/nfp_service/heat/heat_driver.py | 42 - .../nfp/configurator/drivers/vpn/__init__.py | 0 .../configurator/drivers/vpn/vyos/__init__.py | 0 .../drivers/vpn/vyos/vyos_vpn_constants.py | 18 - .../drivers/vpn/vyos/vyos_vpn_driver.py | 1429 -------- .../contrib/nfp/configurator/lib/__init__.py | 0 .../contrib/nfp/configurator/lib/constants.py | 55 - .../nfp/configurator/lib/data_filter.py | 239 -- .../nfp/configurator/lib/data_parser.py | 96 - .../contrib/nfp/configurator/lib/demuxer.py | 165 - .../nfp/configurator/lib/filter_constants.py | 22 - .../nfp/configurator/lib/fw_constants.py | 17 - .../lib/generic_config_constants.py | 35 - .../nfp/configurator/lib/lbv2_constants.py | 113 - .../configurator/lib/nfp_service_constants.py | 20 - .../contrib/nfp/configurator/lib/utils.py | 119 - .../nfp/configurator/lib/vpn_constants.py | 25 - .../nfp/configurator/modules/configurator.py | 542 --- .../contrib/nfp/service_plugins/__init__.py | 0 .../nfp/service_plugins/firewall/__init__.py | 0 .../firewall/nfp_fwaas_plugin.py | 326 -- .../service_plugins/loadbalancer/__init__.py | 0 .../loadbalancer/drivers/__init__.py | 0 .../drivers/nfp_lbaasv2_plugin_driver.py | 27 - .../nfp/service_plugins/vpn/__init__.py | 0 .../service_plugins/vpn/drivers/__init__.py | 0 .../vpn/drivers/nfp_vpnaas_driver.py | 288 -- gbpservice/contrib/nfp/tools/__init__.py | 0 gbpservice/contrib/nfp/tools/setup_nfp.py | 811 ----- gbpservice/contrib/nfp_service/__init__.py | 0 .../reference_configurator/__init__.py | 0 .../controllers/controller.py | 322 -- .../scripts/configure_fw_rules.py | 104 - .../ncp/node_drivers/nfp_node_driver.py | 1091 ------ .../admin_owned_resources_apic_tscp.py | 93 - .../traffic_stitching_plumber.py | 186 -- .../servicechain/__init__.py-disabled | 0 .../base_test_servicechain_plugin.py | 300 -- .../services/servicechain/ncp/__init__.py | 0 .../servicechain/ncp/test_heat_node_driver.py | 624 ---- .../servicechain/ncp/test_ncp_plugin.py | 899 ----- .../servicechain/ncp/test_nfp_node_driver.py | 859 ----- .../ncp/test_traffic_stitching_plumber.py | 168 - .../ncp/test_tscp_resource_mapping.py | 203 -- .../base_configurator/controllers/__init__.py | 58 - .../controllers/controller.py | 219 -- gbpservice/nfp/common/data_formatter.py | 154 - gbpservice/nfp/common/exceptions.py | 137 - gbpservice/nfp/common/topics.py | 17 - gbpservice/nfp/common/utils.py | 122 - gbpservice/nfp/core/__init__.py | 0 gbpservice/nfp/core/cfg.py | 54 - gbpservice/nfp/core/common.py | 68 - gbpservice/nfp/core/context.py | 96 - gbpservice/nfp/core/controller.py | 726 ---- gbpservice/nfp/core/event.py | 412 --- gbpservice/nfp/core/executor.py | 231 -- gbpservice/nfp/core/launcher.py | 106 - gbpservice/nfp/core/log.py | 117 - gbpservice/nfp/core/manager.py | 444 --- gbpservice/nfp/core/module.py | 113 - gbpservice/nfp/core/path.py | 171 - gbpservice/nfp/core/rpc.py | 171 - gbpservice/nfp/core/sequencer.py | 138 - gbpservice/nfp/core/threadpool.py | 99 - gbpservice/nfp/core/watchdog.py | 113 - gbpservice/nfp/core/worker.py | 222 -- gbpservice/nfp/lib/__init__.py | 0 gbpservice/nfp/lib/nfp_context_manager.py | 214 -- gbpservice/nfp/lib/nfp_exceptions.py | 41 - gbpservice/nfp/lib/rest_client_over_unix.py | 171 - gbpservice/nfp/lib/transport.py | 353 -- .../orchestrator/coal/networking/__init__.py | 0 .../coal/networking/nfp_gbp_network_driver.py | 93 - .../networking/nfp_network_driver_base.py | 39 - .../networking/nfp_neutron_network_driver.py | 99 - .../config_drivers/heat_client.py | 70 - .../config_drivers/heat_driver.py | 1906 ----------- gbpservice/nfp/orchestrator/context.py | 520 --- gbpservice/nfp/orchestrator/db/__init__.py | 0 .../nfp/orchestrator/db/common_db_mixin.py | 183 - gbpservice/nfp/orchestrator/db/nfp_db.py | 777 ----- .../nfp/orchestrator/db/nfp_db_model.py | 183 - .../drivers/orchestration_driver.py | 1070 ------ .../modules/device_orchestrator.py | 1880 ----------- .../modules/service_orchestrator.py | 2950 ----------------- .../openstack/openstack_driver.py | 1500 --------- gbpservice/nfp/pecan/api/config.py | 78 - .../nfp/pecan/api/configurator_decider.py | 30 - gbpservice/nfp/pecan/api/root_controller.py | 33 - gbpservice/nfp/pecan/api/setup.py | 39 - gbpservice/nfp/pecan/api/v1/app.py | 23 - gbpservice/nfp/pecan/base_controller.py | 50 - gbpservice/nfp/pecan/constants.py | 35 - gbpservice/nfp/proxy_agent/lib/topics.py | 16 - .../proxy_agent/modules/notification_agent.py | 34 - .../nfp/proxy_agent/modules/proxy_agent.py | 112 - .../nfp/proxy_agent/notifications/pull.py | 97 - gbpservice/nfp/proxy_agent/proxy/proxy.py | 375 --- gbpservice/nfp/utils/forked_pdb.py | 38 - gbpservice/tests/contrib/README | 3 - gbpservice/tests/contrib/__init__.py | 0 .../contrib/devstack/exercises-aim/gbp_aim.sh | 153 - .../devstack/exercises-aim/neutron_aim.sh | 55 - .../contrib/devstack/exercises-nfp/fw.sh | 147 - .../contrib/devstack/exercises-nfp/fw_lb.sh | 207 -- .../contrib/devstack/exercises-nfp/fw_vm.sh | 147 - .../devstack/exercises-nfp/fw_vm_lb.sh | 207 -- .../contrib/devstack/exercises-nfp/lb.sh | 167 - .../contrib/devstack/exercises-nfp/nfp_lib.sh | 20 - .../contrib/devstack/exercises/flavors.sh | 42 - .../tests/contrib/devstack/exercises/gbp.sh | 153 - .../contrib/devstack/exercises/gbp_fip.sh | 115 - .../contrib/devstack/exercises/gbp_purge.sh | 110 - .../contrib/devstack/exercises/gbp_qos.sh | 129 - .../firewall-lb-servicechain/demo.yaml | 126 - .../firewall-lb-servicechain/fw.template | 36 - .../firewall-lb-servicechain/lb.template | 59 - gbpservice/tests/contrib/devstack/lib/gbp | 66 - .../tests/contrib/devstack/local-aim.conf | 23 - .../tests/contrib/devstack/local-nfp.conf | 130 - gbpservice/tests/contrib/devstack/local.conf | 103 - gbpservice/tests/contrib/devstack/nfp | 326 -- .../devstack/nfp-templates/fw_template.yml | 45 - .../nfp-templates/fw_updated_template.yml | 31 - .../devstack/nfp-templates/haproxy.template | 65 - .../nfp-templates/haproxy_lbaasv2.template | 83 - gbpservice/tests/contrib/functions-gbp | 151 - gbpservice/tests/contrib/gate_aim_hook.sh | 23 - gbpservice/tests/contrib/gate_hook.sh | 23 - gbpservice/tests/contrib/gate_nfp_hook.sh | 40 - .../tests/contrib/gbpfunctests/README.md | 20 - .../contrib/gbpfunctests/libs/__init__.py | 0 .../contrib/gbpfunctests/libs/config_libs.py | 403 --- .../contrib/gbpfunctests/libs/utils_libs.py | 108 - .../contrib/gbpfunctests/libs/verify_libs.py | 458 --- .../gbpfunctests/testcases/__init__.py | 0 .../gbpfunctests/testcases/func_neg.txt | 0 .../gbpfunctests/testcases/fw.template | 36 - .../gbpfunctests/testcases/lb.template | 73 - .../testcases/lb_multiple_members.template | 101 - .../contrib/gbpfunctests/testcases/run_suite | 30 - .../gbpfunctests/testcases/suite_admin_run.py | 49 - .../testcases/suite_non_admin_run.py | 76 - .../gbpfunctests/testcases/suite_run.py | 69 - .../gbpfunctests/testcases/tc_gbp_l2p_func.py | 311 -- .../gbpfunctests/testcases/tc_gbp_l3p_func.py | 462 --- .../gbpfunctests/testcases/tc_gbp_l3p_neg.py | 307 -- .../gbpfunctests/testcases/tc_gbp_nsp_func.py | 442 --- .../gbpfunctests/testcases/tc_gbp_pa_func.py | 253 -- .../gbpfunctests/testcases/tc_gbp_pa_neg.py | 239 -- .../gbpfunctests/testcases/tc_gbp_pc_func.py | 439 --- .../gbpfunctests/testcases/tc_gbp_pc_neg.py | 267 -- .../gbpfunctests/testcases/tc_gbp_pr_func.py | 581 ---- .../gbpfunctests/testcases/tc_gbp_pr_neg.py | 222 -- .../testcases/tc_gbp_pr_pc_pa_shared_func.py | 557 ---- .../gbpfunctests/testcases/tc_gbp_prs_func.py | 643 ---- .../testcases/tc_gbp_prs_func.py.aim | 637 ---- .../gbpfunctests/testcases/tc_gbp_prs_neg.py | 207 -- .../testcases/tc_gbp_prs_pr_shared_func.py | 309 -- .../tc_gbp_prs_pr_shared_func.py.aim | 289 -- .../gbpfunctests/testcases/tc_gbp_ptg_func.py | 456 --- .../testcases/tc_gbp_ri_func_1.py | 188 -- .../testcases/tc_gbp_ri_func_2.py | 145 - .../testcases/tc_gbp_ri_func_3.py | 118 - .../testcases/tc_gbp_ri_func_4.py | 109 - .../gbpfunctests/testcases/test_results.txt | 0 .../testcases/test_results_admin.txt | 0 gbpservice/tests/contrib/post_test_hook.sh | 47 - .../tests/contrib/post_test_nfp_hook.sh | 25 - .../tests/contrib/post_test_rally_hook.sh | 15 - 214 files changed, 45606 deletions(-) delete mode 100644 gbpservice/contrib/nfp/__init__.py delete mode 100644 gbpservice/contrib/nfp/config_orchestrator/__init__.py delete mode 100644 gbpservice/contrib/nfp/config_orchestrator/common/__init__.py delete mode 100644 gbpservice/contrib/nfp/config_orchestrator/common/common.py delete mode 100644 gbpservice/contrib/nfp/config_orchestrator/common/lbv2_constants.py delete mode 100644 gbpservice/contrib/nfp/config_orchestrator/common/topics.py delete mode 100644 gbpservice/contrib/nfp/config_orchestrator/handlers/__init__.py delete mode 100644 gbpservice/contrib/nfp/config_orchestrator/handlers/config/__init__.py delete mode 100644 gbpservice/contrib/nfp/config_orchestrator/handlers/config/firewall.py delete mode 100644 gbpservice/contrib/nfp/config_orchestrator/handlers/config/loadbalancerv2.py delete mode 100644 gbpservice/contrib/nfp/config_orchestrator/handlers/config/vpn.py delete mode 100644 gbpservice/contrib/nfp/config_orchestrator/handlers/notification/__init__.py delete mode 100644 gbpservice/contrib/nfp/config_orchestrator/handlers/notification/handler.py delete mode 100644 gbpservice/contrib/nfp/config_orchestrator/modules/__init__.py delete mode 100644 gbpservice/contrib/nfp/config_orchestrator/modules/config_orch.py delete mode 100644 gbpservice/contrib/nfp/configurator/__init__.py delete mode 100644 gbpservice/contrib/nfp/configurator/advanced_controller/__init__.py delete mode 100644 gbpservice/contrib/nfp/configurator/advanced_controller/controller.py delete mode 100644 gbpservice/contrib/nfp/configurator/advanced_controller/controller_loader.py delete mode 100644 gbpservice/contrib/nfp/configurator/agents/__init__.py delete mode 100644 gbpservice/contrib/nfp/configurator/agents/agent_base.py delete mode 100644 gbpservice/contrib/nfp/configurator/agents/firewall.py delete mode 100644 gbpservice/contrib/nfp/configurator/agents/generic_config.py delete mode 100644 gbpservice/contrib/nfp/configurator/agents/loadbalancer_v2.py delete mode 100644 gbpservice/contrib/nfp/configurator/agents/nfp_service.py delete mode 100644 gbpservice/contrib/nfp/configurator/agents/vpn.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/__init__.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/base/__init__.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/base/base_driver.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/firewall/__init__.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/firewall/vyos/__init__.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/firewall/vyos/vyos_fw_constants.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/firewall/vyos/vyos_fw_driver.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/loadbalancer/__init__.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/__init__.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/common/neutron_lbaas_data_models.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/config.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/haproxy_driver.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/haproxy_driver_constants.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/local_cert_manager.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/rest_api_driver.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/nfp_service/__init__.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/nfp_service/heat/__init__.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/nfp_service/heat/heat_driver.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/vpn/__init__.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/vpn/vyos/__init__.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/vpn/vyos/vyos_vpn_constants.py delete mode 100644 gbpservice/contrib/nfp/configurator/drivers/vpn/vyos/vyos_vpn_driver.py delete mode 100644 gbpservice/contrib/nfp/configurator/lib/__init__.py delete mode 100644 gbpservice/contrib/nfp/configurator/lib/constants.py delete mode 100644 gbpservice/contrib/nfp/configurator/lib/data_filter.py delete mode 100644 gbpservice/contrib/nfp/configurator/lib/data_parser.py delete mode 100644 gbpservice/contrib/nfp/configurator/lib/demuxer.py delete mode 100644 gbpservice/contrib/nfp/configurator/lib/filter_constants.py delete mode 100644 gbpservice/contrib/nfp/configurator/lib/fw_constants.py delete mode 100644 gbpservice/contrib/nfp/configurator/lib/generic_config_constants.py delete mode 100644 gbpservice/contrib/nfp/configurator/lib/lbv2_constants.py delete mode 100644 gbpservice/contrib/nfp/configurator/lib/nfp_service_constants.py delete mode 100644 gbpservice/contrib/nfp/configurator/lib/utils.py delete mode 100644 gbpservice/contrib/nfp/configurator/lib/vpn_constants.py delete mode 100644 gbpservice/contrib/nfp/configurator/modules/configurator.py delete mode 100644 gbpservice/contrib/nfp/service_plugins/__init__.py delete mode 100644 gbpservice/contrib/nfp/service_plugins/firewall/__init__.py delete mode 100644 gbpservice/contrib/nfp/service_plugins/firewall/nfp_fwaas_plugin.py delete mode 100644 gbpservice/contrib/nfp/service_plugins/loadbalancer/__init__.py delete mode 100644 gbpservice/contrib/nfp/service_plugins/loadbalancer/drivers/__init__.py delete mode 100644 gbpservice/contrib/nfp/service_plugins/loadbalancer/drivers/nfp_lbaasv2_plugin_driver.py delete mode 100644 gbpservice/contrib/nfp/service_plugins/vpn/__init__.py delete mode 100644 gbpservice/contrib/nfp/service_plugins/vpn/drivers/__init__.py delete mode 100644 gbpservice/contrib/nfp/service_plugins/vpn/drivers/nfp_vpnaas_driver.py delete mode 100644 gbpservice/contrib/nfp/tools/__init__.py delete mode 100755 gbpservice/contrib/nfp/tools/setup_nfp.py delete mode 100644 gbpservice/contrib/nfp_service/__init__.py delete mode 100644 gbpservice/contrib/nfp_service/reference_configurator/__init__.py delete mode 100644 gbpservice/contrib/nfp_service/reference_configurator/controllers/controller.py delete mode 100644 gbpservice/contrib/nfp_service/reference_configurator/scripts/configure_fw_rules.py delete mode 100644 gbpservice/neutron/services/servicechain/plugins/ncp/node_plumbers/admin_owned_resources_apic_tscp.py delete mode 100644 gbpservice/neutron/services/servicechain/plugins/ncp/node_plumbers/traffic_stitching_plumber.py delete mode 100644 gbpservice/neutron/tests/unit/services/servicechain/__init__.py-disabled delete mode 100644 gbpservice/neutron/tests/unit/services/servicechain/base_test_servicechain_plugin.py delete mode 100644 gbpservice/neutron/tests/unit/services/servicechain/ncp/__init__.py delete mode 100644 gbpservice/neutron/tests/unit/services/servicechain/ncp/test_heat_node_driver.py delete mode 100644 gbpservice/neutron/tests/unit/services/servicechain/ncp/test_ncp_plugin.py delete mode 100644 gbpservice/neutron/tests/unit/services/servicechain/ncp/test_nfp_node_driver.py delete mode 100644 gbpservice/neutron/tests/unit/services/servicechain/ncp/test_traffic_stitching_plumber.py delete mode 100644 gbpservice/neutron/tests/unit/services/servicechain/ncp/test_tscp_resource_mapping.py delete mode 100644 gbpservice/nfp/base_configurator/controllers/__init__.py delete mode 100644 gbpservice/nfp/base_configurator/controllers/controller.py delete mode 100644 gbpservice/nfp/common/data_formatter.py delete mode 100644 gbpservice/nfp/common/exceptions.py delete mode 100644 gbpservice/nfp/common/topics.py delete mode 100644 gbpservice/nfp/common/utils.py delete mode 100644 gbpservice/nfp/core/__init__.py delete mode 100644 gbpservice/nfp/core/cfg.py delete mode 100644 gbpservice/nfp/core/common.py delete mode 100644 gbpservice/nfp/core/context.py delete mode 100644 gbpservice/nfp/core/controller.py delete mode 100644 gbpservice/nfp/core/event.py delete mode 100644 gbpservice/nfp/core/executor.py delete mode 100644 gbpservice/nfp/core/launcher.py delete mode 100644 gbpservice/nfp/core/log.py delete mode 100644 gbpservice/nfp/core/manager.py delete mode 100644 gbpservice/nfp/core/module.py delete mode 100644 gbpservice/nfp/core/path.py delete mode 100644 gbpservice/nfp/core/rpc.py delete mode 100644 gbpservice/nfp/core/sequencer.py delete mode 100644 gbpservice/nfp/core/threadpool.py delete mode 100644 gbpservice/nfp/core/watchdog.py delete mode 100644 gbpservice/nfp/core/worker.py delete mode 100644 gbpservice/nfp/lib/__init__.py delete mode 100644 gbpservice/nfp/lib/nfp_context_manager.py delete mode 100644 gbpservice/nfp/lib/nfp_exceptions.py delete mode 100644 gbpservice/nfp/lib/rest_client_over_unix.py delete mode 100644 gbpservice/nfp/lib/transport.py delete mode 100644 gbpservice/nfp/orchestrator/coal/networking/__init__.py delete mode 100644 gbpservice/nfp/orchestrator/coal/networking/nfp_gbp_network_driver.py delete mode 100644 gbpservice/nfp/orchestrator/coal/networking/nfp_network_driver_base.py delete mode 100644 gbpservice/nfp/orchestrator/coal/networking/nfp_neutron_network_driver.py delete mode 100644 gbpservice/nfp/orchestrator/config_drivers/heat_client.py delete mode 100644 gbpservice/nfp/orchestrator/config_drivers/heat_driver.py delete mode 100644 gbpservice/nfp/orchestrator/context.py delete mode 100644 gbpservice/nfp/orchestrator/db/__init__.py delete mode 100644 gbpservice/nfp/orchestrator/db/common_db_mixin.py delete mode 100644 gbpservice/nfp/orchestrator/db/nfp_db.py delete mode 100644 gbpservice/nfp/orchestrator/db/nfp_db_model.py delete mode 100644 gbpservice/nfp/orchestrator/drivers/orchestration_driver.py delete mode 100644 gbpservice/nfp/orchestrator/modules/device_orchestrator.py delete mode 100644 gbpservice/nfp/orchestrator/modules/service_orchestrator.py delete mode 100644 gbpservice/nfp/orchestrator/openstack/openstack_driver.py delete mode 100644 gbpservice/nfp/pecan/api/config.py delete mode 100644 gbpservice/nfp/pecan/api/configurator_decider.py delete mode 100644 gbpservice/nfp/pecan/api/root_controller.py delete mode 100644 gbpservice/nfp/pecan/api/setup.py delete mode 100644 gbpservice/nfp/pecan/api/v1/app.py delete mode 100644 gbpservice/nfp/pecan/base_controller.py delete mode 100644 gbpservice/nfp/pecan/constants.py delete mode 100644 gbpservice/nfp/proxy_agent/lib/topics.py delete mode 100644 gbpservice/nfp/proxy_agent/modules/notification_agent.py delete mode 100644 gbpservice/nfp/proxy_agent/modules/proxy_agent.py delete mode 100644 gbpservice/nfp/proxy_agent/notifications/pull.py delete mode 100644 gbpservice/nfp/proxy_agent/proxy/proxy.py delete mode 100644 gbpservice/nfp/utils/forked_pdb.py delete mode 100644 gbpservice/tests/contrib/README delete mode 100644 gbpservice/tests/contrib/__init__.py delete mode 100755 gbpservice/tests/contrib/devstack/exercises-aim/gbp_aim.sh delete mode 100755 gbpservice/tests/contrib/devstack/exercises-aim/neutron_aim.sh delete mode 100755 gbpservice/tests/contrib/devstack/exercises-nfp/fw.sh delete mode 100755 gbpservice/tests/contrib/devstack/exercises-nfp/fw_lb.sh delete mode 100755 gbpservice/tests/contrib/devstack/exercises-nfp/fw_vm.sh delete mode 100755 gbpservice/tests/contrib/devstack/exercises-nfp/fw_vm_lb.sh delete mode 100755 gbpservice/tests/contrib/devstack/exercises-nfp/lb.sh delete mode 100755 gbpservice/tests/contrib/devstack/exercises-nfp/nfp_lib.sh delete mode 100755 gbpservice/tests/contrib/devstack/exercises/flavors.sh delete mode 100755 gbpservice/tests/contrib/devstack/exercises/gbp.sh delete mode 100755 gbpservice/tests/contrib/devstack/exercises/gbp_fip.sh delete mode 100755 gbpservice/tests/contrib/devstack/exercises/gbp_purge.sh delete mode 100755 gbpservice/tests/contrib/devstack/exercises/gbp_qos.sh delete mode 100644 gbpservice/tests/contrib/devstack/gbp-templates/firewall-lb-servicechain/demo.yaml delete mode 100644 gbpservice/tests/contrib/devstack/gbp-templates/firewall-lb-servicechain/fw.template delete mode 100644 gbpservice/tests/contrib/devstack/gbp-templates/firewall-lb-servicechain/lb.template delete mode 100644 gbpservice/tests/contrib/devstack/lib/gbp delete mode 100644 gbpservice/tests/contrib/devstack/local-aim.conf delete mode 100644 gbpservice/tests/contrib/devstack/local-nfp.conf delete mode 100644 gbpservice/tests/contrib/devstack/local.conf delete mode 100644 gbpservice/tests/contrib/devstack/nfp delete mode 100644 gbpservice/tests/contrib/devstack/nfp-templates/fw_template.yml delete mode 100644 gbpservice/tests/contrib/devstack/nfp-templates/fw_updated_template.yml delete mode 100644 gbpservice/tests/contrib/devstack/nfp-templates/haproxy.template delete mode 100644 gbpservice/tests/contrib/devstack/nfp-templates/haproxy_lbaasv2.template delete mode 100644 gbpservice/tests/contrib/functions-gbp delete mode 100644 gbpservice/tests/contrib/gate_aim_hook.sh delete mode 100644 gbpservice/tests/contrib/gate_hook.sh delete mode 100644 gbpservice/tests/contrib/gate_nfp_hook.sh delete mode 100755 gbpservice/tests/contrib/gbpfunctests/README.md delete mode 100755 gbpservice/tests/contrib/gbpfunctests/libs/__init__.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/libs/config_libs.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/libs/utils_libs.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/libs/verify_libs.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/__init__.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/func_neg.txt delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/fw.template delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/lb.template delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/lb_multiple_members.template delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/run_suite delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/suite_admin_run.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/suite_non_admin_run.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/suite_run.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_l2p_func.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_l3p_func.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_l3p_neg.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_nsp_func.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pa_func.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pa_neg.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pc_func.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pc_neg.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pr_func.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pr_neg.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pr_pc_pa_shared_func.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_func.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_func.py.aim delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_neg.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_pr_shared_func.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_pr_shared_func.py.aim delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ptg_func.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ri_func_1.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ri_func_2.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ri_func_3.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ri_func_4.py delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/test_results.txt delete mode 100755 gbpservice/tests/contrib/gbpfunctests/testcases/test_results_admin.txt delete mode 100644 gbpservice/tests/contrib/post_test_hook.sh delete mode 100644 gbpservice/tests/contrib/post_test_nfp_hook.sh delete mode 100644 gbpservice/tests/contrib/post_test_rally_hook.sh diff --git a/gbpservice/contrib/nfp/__init__.py b/gbpservice/contrib/nfp/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/config_orchestrator/__init__.py b/gbpservice/contrib/nfp/config_orchestrator/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/config_orchestrator/common/__init__.py b/gbpservice/contrib/nfp/config_orchestrator/common/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/config_orchestrator/common/common.py b/gbpservice/contrib/nfp/config_orchestrator/common/common.py deleted file mode 100644 index 93cc9eebc..000000000 --- a/gbpservice/contrib/nfp/config_orchestrator/common/common.py +++ /dev/null @@ -1,189 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from gbpservice.contrib.nfp.config_orchestrator.common import ( - topics as a_topics) -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.lib import transport -from gbpservice.nfp.orchestrator.openstack import openstack_driver - -from neutron_lib.agent import topics as n_topics -from neutron_lib import constants as n_constants -from neutron_lib import rpc as n_rpc - -import oslo_messaging as messaging - -LOG = nfp_logging.getLogger(__name__) - - -def prepare_request_data(context, resource, resource_type, - resource_data, service_vendor=None): - - request_data = {'info': { - 'context': context, - 'service_type': resource_type, - 'service_vendor': service_vendor - }, - - 'config': [{ - 'resource': resource, - 'resource_data': resource_data - }] - } - - return request_data - - -def _filter_data(routers, networks, filters): - # filter routers and networks data and formulate - # dictionary of subnets, routers and ports for the - # given tenant. - tenant_id = filters['tenant_id'][0] - _filtered_routers = [] - _filtered_subnets = [] - _filtered_ports = [] - _filtered_networks = [] - for router in routers: - if router['tenant_id'] == tenant_id: - _filtered_routers.append({'id': router['id']}) - for network in networks: - if network['tenant_id'] == tenant_id: - subnets = network['subnets'] - ports = network['ports'] - _filtered_networks.append( - {'id': network['id'], - 'tenant_id': network['tenant_id'], - 'provider:segmentation_id': network[ - 'provider:segmentation_id'], - 'provider:network_type': network[ - 'provider:network_type'], - 'shared': network['shared'], - 'router:external': network['router:external']}) - for subnet in subnets: - if subnet['tenant_id'] == tenant_id: - _filtered_subnets.append( - {'id': subnet['id'], - 'cidr': subnet['cidr'], - 'gateway_ip': subnet['gateway_ip'], - 'network_id': subnet['network_id']}) - for port in ports: - if port['tenant_id'] == tenant_id: - _filtered_ports.append( - {'id': port['id'], - 'fixed_ips': port['fixed_ips'], - 'binding:host_id': port['binding:host_id'], - 'network_id': port['network_id']}) - - return {'subnets': _filtered_subnets, - 'routers': _filtered_routers, - 'ports': _filtered_ports, - 'networks': _filtered_networks} - - -def get_core_context(context, filters, config): - routers = [] - networks = get_networks(context, config) - return _filter_data(routers, networks, filters) - - -def get_dhcp_agent_host(config): - try: - neutronclient = openstack_driver.NeutronClient(config) - keystoneclient = openstack_driver.KeystoneClient(config) - token = keystoneclient.get_admin_token() - filters = {'agent_type': 'DHCP agent', 'alive': True} - agents = neutronclient.get_agents(token, filters) - if agents: - return agents[0].get('host', None) - except Exception as exc: - LOG.error("Failed to get dhcp agent host : %(exc)s", - {'exc': exc}) - - -def get_networks(context, config): - host = get_dhcp_agent_host(config) - if not host: - return [] - target = messaging.Target( - topic=n_topics.PLUGIN, - namespace=n_constants.RPC_NAMESPACE_DHCP_PLUGIN, - version='1.0') - client = n_rpc.get_client(target) - cctxt = client.prepare(version='1.1') - return cctxt.call(context, 'get_active_networks_info', - host=host) - - -def _prepare_structure(network_function_details, ports_info, - mngmt_port_info, monitor_port_info): - return {'nfi_ports_map': { - network_function_details[ - 'network_function_instance'][ - 'id']: ports_info}, - 'nfi_nfd_map': { - network_function_details[ - 'network_function_instance'][ - 'id']: { - 'nfd': network_function_details[ - 'network_function_device'], - 'nfd_mgmt_port': mngmt_port_info, - 'nfd_monitoring_port': None, - 'nfd_monitoring_port_network': network_function_details[ - 'network_function_device'][ - 'monitoring_port_network']}}, - 'nfi': [network_function_details['network_function_instance']], - 'nf': network_function_details['network_function'] - } - - -def get_network_function_details(context, network_function_id): - network_function_details = None - try: - rpc_nso_client = transport.RPCClient(a_topics.NFP_NSO_TOPIC) - network_function_details = rpc_nso_client.cctxt.call( - context, - 'get_network_function_details', - network_function_id=network_function_id) - msg = (" %s " % (network_function_details)) - LOG.debug(msg) - return network_function_details['network_function'] - - except Exception as e: - LOG.error("Failed to get network function details of " - "network_function_id %(network_function_id)s : %(ex)s ", - {'ex': e, 'network_function_id': network_function_id}) - - -def get_network_function_map(context, network_function_id): - request_data = None - try: - rpc_nso_client = transport.RPCClient(a_topics.NFP_NSO_TOPIC) - nf_context = rpc_nso_client.cctxt.call( - context, - 'get_network_function_context', - network_function_id=network_function_id) - - network_function_details = nf_context['network_function_details'] - ports_info = nf_context['ports_info'] - mngmt_port_info = nf_context['mngmt_port_info'] - monitor_port_info = nf_context['monitor_port_info'] - - request_data = _prepare_structure(network_function_details, ports_info, - mngmt_port_info, monitor_port_info) - msg = (" %s " % (request_data)) - LOG.debug(msg) - return request_data - except Exception as e: - LOG.error("Failed to get network function map of " - "network_function_id %(network_function_id)s : %(ex)s ", - {'ex': e, 'network_function_id': network_function_id}) - return request_data diff --git a/gbpservice/contrib/nfp/config_orchestrator/common/lbv2_constants.py b/gbpservice/contrib/nfp/config_orchestrator/common/lbv2_constants.py deleted file mode 100644 index 88d3a0688..000000000 --- a/gbpservice/contrib/nfp/config_orchestrator/common/lbv2_constants.py +++ /dev/null @@ -1,20 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -# Constants to extend status strings in neutron.plugins.common.constants -ONLINE = 'ONLINE' -OFFLINE = 'OFFLINE' -DEGRADED = 'DEGRADED' -DISABLED = 'DISABLED' -NO_MONITOR = 'NO_MONITOR' - -PROTOCOL_TERMINATED_HTTPS = 'TERMINATED_HTTPS' diff --git a/gbpservice/contrib/nfp/config_orchestrator/common/topics.py b/gbpservice/contrib/nfp/config_orchestrator/common/topics.py deleted file mode 100644 index 026038970..000000000 --- a/gbpservice/contrib/nfp/config_orchestrator/common/topics.py +++ /dev/null @@ -1,23 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -FW_NFP_CONFIGAGENT_TOPIC = 'nfp-firewall-agent' -LBV2_NFP_CONFIGAGENT_TOPIC = 'nfp-lbaasv2-agent' -VPN_NFP_CONFIGAGENT_TOPIC = 'nfp-vpn_agent' -NFP_NSO_TOPIC = "nfp-service-orchestrator" - -FW_NFP_PLUGIN_TOPIC = 'q-firewall-plugin' -LBV2_NFP_PLUGIN_TOPIC = 'n-lbaasv2-plugin' -VPN_NFP_PLUGIN_TOPIC = 'vpn_plugin' -DEVICE_ORCH_TOPIC = 'nfp-configurator-ndo' -SERVICE_ORCH_TOPIC = 'nfp-configurator-nso' -CONFIG_ORCH_TOPIC = 'nfp-nco-notification-topic' diff --git a/gbpservice/contrib/nfp/config_orchestrator/handlers/__init__.py b/gbpservice/contrib/nfp/config_orchestrator/handlers/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/config_orchestrator/handlers/config/__init__.py b/gbpservice/contrib/nfp/config_orchestrator/handlers/config/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/config_orchestrator/handlers/config/firewall.py b/gbpservice/contrib/nfp/config_orchestrator/handlers/config/firewall.py deleted file mode 100644 index e1e47c07a..000000000 --- a/gbpservice/contrib/nfp/config_orchestrator/handlers/config/firewall.py +++ /dev/null @@ -1,179 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import ast -import copy - -from gbpservice.contrib.nfp.config_orchestrator.common import common -from gbpservice.nfp.common import constants as const -from gbpservice.nfp.common import data_formatter as df -from gbpservice.nfp.common import utils -from gbpservice.nfp.core import context as module_context -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.lib import transport - -from neutron_fwaas.db.firewall import firewall_db - -from oslo_log import helpers as log_helpers -import oslo_messaging as messaging - -LOG = nfp_logging.getLogger(__name__) - -""" -RPC handler for Firewall service -""" - - -class FwAgent(firewall_db.Firewall_db_mixin): - - RPC_API_VERSION = '1.0' - target = messaging.Target(version=RPC_API_VERSION) - - def __init__(self, conf, sc): - super(FwAgent, self).__init__() - self._conf = conf - self._sc = sc - self._db_inst = super(FwAgent, self) - - def _get_firewalls(self, context, tenant_id, - firewall_policy_id, description): - filters = {'tenant_id': [tenant_id], - 'firewall_policy_id': [firewall_policy_id]} - args = {'context': context, 'filters': filters} - firewalls = self._db_inst.get_firewalls(**args) - for firewall in firewalls: - firewall['description'] = description - return firewalls - - def _get_firewall_policies(self, context, tenant_id, - firewall_policy_id, description): - filters = {'tenant_id': [tenant_id], - 'id': [firewall_policy_id]} - args = {'context': context, 'filters': filters} - firewall_policies = self._db_inst.get_firewall_policies(**args) - return firewall_policies - - def _get_firewall_rules(self, context, tenant_id, - firewall_policy_id, description): - filters = {'tenant_id': [tenant_id], - 'firewall_policy_id': [firewall_policy_id]} - args = {'context': context, 'filters': filters} - firewall_rules = self._db_inst.get_firewall_rules(**args) - return firewall_rules - - def _get_firewall_context(self, **kwargs): - firewalls = self._get_firewalls(**kwargs) - firewall_policies = self._get_firewall_policies(**kwargs) - firewall_rules = self._get_firewall_rules(**kwargs) - return {'firewalls': firewalls, - 'firewall_policies': firewall_policies, - 'firewall_rules': firewall_rules} - - def _context(self, **kwargs): - context = kwargs.get('context') - if context.is_admin: - kwargs['tenant_id'] = context.tenant_id - db = self._get_firewall_context(**kwargs) - return db - - def _prepare_resource_context_dicts(self, **kwargs): - # Prepare context_dict - context = kwargs.get('context') - context_resource_data = kwargs.pop('context_resource_data') - ctx_dict = context.to_dict() - # Collecting db entry required by configurator. - # Addind service_info to neutron context and sending - # dictionary format to the configurator. - db = self._context(**kwargs) - rsrc_ctx_dict = copy.deepcopy(ctx_dict) - rsrc_ctx_dict.update({'service_info': db}) - rsrc_ctx_dict.update({'resource_data': context_resource_data}) - return ctx_dict, rsrc_ctx_dict - - def _get_resource_data(self, description, resource_type): - resource_data = df.get_network_function_info(description, - resource_type) - return resource_data - - def _update_request_data(self, body, description): - pass - - def _data_wrapper(self, context, firewall, host, nf, reason): - # Hardcoding the position for fetching data since we are owning - # its positional change - description = ast.literal_eval((nf['description'].split('\n'))[1]) - description.update({'tenant_id': firewall['tenant_id']}) - context_resource_data = self._get_resource_data(description, - const.FIREWALL) - fw_mac = description['provider_ptg_info'][0] - # REVISIT(dpak): We need to avoid resource description - # dependency in OTC and instead use neutron context description. - firewall.update({'description': str(description)}) - kwargs = {'context': context, - 'context_resource_data': context_resource_data, - 'firewall_policy_id': firewall[ - 'firewall_policy_id'], - 'description': str(description), - 'tenant_id': firewall['tenant_id']} - - ctx_dict, rsrc_ctx_dict = self._prepare_resource_context_dicts( - **kwargs) - service_vm_context = utils.get_service_vm_context( - description['service_vendor']) - nfp_context = {'network_function_id': nf['id'], - 'neutron_context': ctx_dict, - 'fw_mac': fw_mac, - 'requester': 'nas_service', - 'logging_context': module_context.get()['log_context'], - 'service_vm_context': service_vm_context} - resource = resource_type = 'firewall' - resource_data = {resource: firewall, - 'host': host, - 'neutron_context': rsrc_ctx_dict} - body = common.prepare_request_data(nfp_context, resource, - resource_type, resource_data, - description['service_vendor']) - self._update_request_data(body, description) - return body - - def _fetch_nf_from_resource_desc(self, desc): - desc_dict = ast.literal_eval(desc) - nf_id = desc_dict['network_function_id'] - return nf_id - - @log_helpers.log_method_call - def create_firewall(self, context, firewall, host): - nfp_context = module_context.init() - # Fetch nf_id from description of the resource - nf_id = self._fetch_nf_from_resource_desc(firewall["description"]) - nfp_context['log_context']['meta_id'] = nf_id - nf = common.get_network_function_details(context, nf_id) - LOG.info("Received RPC CREATE FIREWALL for " - "Firewall: %(firewall)s", - {'firewall': firewall}) - body = self._data_wrapper(context, firewall, host, nf, 'CREATE') - transport.send_request_to_configurator(self._conf, - context, body, "CREATE") - - @log_helpers.log_method_call - def delete_firewall(self, context, firewall, host): - nfp_context = module_context.init() - # Fetch nf_id from description of the resource - nf_id = self._fetch_nf_from_resource_desc(firewall["description"]) - nfp_context['log_context']['meta_id'] = nf_id - nf = common.get_network_function_details(context, nf_id) - LOG.info("Received RPC DELETE FIREWALL for " - "Firewall: %(firewall)s", - {'firewall': firewall}) - body = self._data_wrapper(context, firewall, host, nf, 'DELETE') - transport.send_request_to_configurator(self._conf, - context, body, "DELETE") diff --git a/gbpservice/contrib/nfp/config_orchestrator/handlers/config/loadbalancerv2.py b/gbpservice/contrib/nfp/config_orchestrator/handlers/config/loadbalancerv2.py deleted file mode 100644 index 424265d6e..000000000 --- a/gbpservice/contrib/nfp/config_orchestrator/handlers/config/loadbalancerv2.py +++ /dev/null @@ -1,523 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import ast -import copy - -from gbpservice.contrib.nfp.config_orchestrator.common import common -from gbpservice.contrib.nfp.config_orchestrator.common import lbv2_constants -from gbpservice.nfp.common import constants as const -from gbpservice.nfp.common import data_formatter as df -from gbpservice.nfp.common import utils -from gbpservice.nfp.core import context as module_context -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.lib import transport - -from neutron_lbaas.common import cert_manager -from neutron_lbaas.common.tls_utils import cert_parser -from neutron_lbaas.db.loadbalancer import loadbalancer_dbv2 -from neutron_lbaas.extensions import loadbalancerv2 - -from oslo_log import helpers as log_helpers -import oslo_messaging as messaging - -LOG = nfp_logging.getLogger(__name__) - -""" -RPC handler for Loadbalancer service -""" - - -class Lbv2Agent(loadbalancer_dbv2.LoadBalancerPluginDbv2): - target = messaging.Target(version=const.LOADBALANCERV2_RPC_API_VERSION) - - def __init__(self, conf, sc): - super(Lbv2Agent, self).__init__() - self._conf = conf - self._sc = sc - self._cert_manager_plugin = cert_manager.get_backend() - self._db_inst = super(Lbv2Agent, self) - - def _filter_service_info_with_resource(self, lb_db, core_db): - updated_db = {'subnets': [], - 'ports': []} - for lb in lb_db['loadbalancers']: - lb_port_id = lb['vip_port_id'] - lb_subnet_id = lb['vip_subnet_id'] - for subnet in core_db['subnets']: - if subnet['id'] == lb_subnet_id: - updated_db['subnets'].append(subnet) - for port in core_db['ports']: - if port['id'] == lb_port_id: - updated_db['ports'].append(port) - lb_db.update(updated_db) - return lb_db - - def _to_api_dict(self, objs): - ret_list = [] - for obj in objs: - ret_list.append(obj.to_api_dict()) - return ret_list - - def _get_core_context(self, context, tenant_id): - filters = {'tenant_id': [tenant_id]} - core_context_dict = common.get_core_context(context, - filters, - self._conf) - del core_context_dict['routers'] - return core_context_dict - - def _get_lb_context(self, context, filters): - args = {'context': context, 'filters': filters} - db_data = super(Lbv2Agent, self) - return {'loadbalancers': self._to_api_dict( - db_data.get_loadbalancers(**args)), - 'listeners': self._to_api_dict( - db_data.get_listeners(**args)), - 'pools': self._to_api_dict( - db_data.get_pools(**args)), - 'pool_members': self._to_api_dict( - db_data.get_pool_members(**args)), - 'healthmonitors': self._to_api_dict( - db_data.get_healthmonitors(**args))} - - def _context(self, **kwargs): - context = kwargs.get('context') - if context.is_admin: - kwargs['tenant_id'] = context.tenant_id - core_db = self._get_core_context(context, kwargs['tenant_id']) - # REVISIT(jiahao): _get_lb_context() fails for flavor_id, disable it - # for now. Sent the whole core_db to configurator - # lb_db = self._get_lb_context(**kwargs) - # db = self._filter_service_info_with_resource(lb_db, core_db) - db = core_db - return db - - def _prepare_resource_context_dicts(self, **kwargs): - # Prepare context_dict - context = kwargs.get('context') - context_resource_data = kwargs.pop('context_resource_data') - ctx_dict = context.to_dict() - # Collecting db entry required by configurator. - # Addind service_info to neutron context and sending - # dictionary format to the configurator. - db = self._context(**kwargs) - rsrc_ctx_dict = copy.deepcopy(ctx_dict) - rsrc_ctx_dict.update({'service_info': db}) - rsrc_ctx_dict.update({'resource_data': context_resource_data}) - return ctx_dict, rsrc_ctx_dict - - def _data_wrapper(self, context, tenant_id, name, reason, nf, **kwargs): - nfp_context = {} - description = ast.literal_eval((nf['description'].split('\n'))[1]) - description.update({'tenant_id': tenant_id}) - context_resource_data = df.get_network_function_info( - description, const.LOADBALANCERV2) - # REVISIT(dpak): We need to avoid resource description - # dependency in OTC and instead use neutron context description. - if name.lower() == 'loadbalancer': - lb_id = kwargs['loadbalancer']['id'] - kwargs['loadbalancer'].update({'description': str(description)}) - nfp_context = {'network_function_id': nf['id'], - 'loadbalancer_id': kwargs['loadbalancer']['id']} - elif name.lower() == 'listener': - lb_id = kwargs['listener'].get('loadbalancer_id') - kwargs['listener']['description'] = str(description) - elif name.lower() == 'pool': - lb_id = kwargs['pool'].get('loadbalancer_id') - kwargs['pool']['description'] = str(description) - elif name.lower() == 'member': - pool = kwargs['member'].get('pool') - if pool: - lb_id = pool.get('loadbalancer_id') - kwargs['member']['description'] = str(description) - elif name.lower() == 'healthmonitor': - pool = kwargs['healthmonitor'].get('pool') - if pool: - lb_id = pool.get('loadbalancer_id') - kwargs['healthmonitor']['description'] = str(description) - else: - kwargs[name.lower()].update({'description': str(description)}) - lb_id = kwargs[name.lower()].get('loadbalancer_id') - - args = {'tenant_id': tenant_id, - 'lb_id': lb_id, - 'context': context, - 'description': str(description), - 'context_resource_data': context_resource_data} - - ctx_dict, rsrc_ctx_dict = self._prepare_resource_context_dicts(**args) - service_vm_context = utils.get_service_vm_context( - description['service_vendor']) - nfp_context.update({'neutron_context': ctx_dict, - 'requester': 'nas_service', - 'logging_context': - module_context.get()['log_context'], - 'service_vm_context': service_vm_context}) - resource_type = 'loadbalancerv2' - resource = name - resource_data = {'neutron_context': rsrc_ctx_dict} - resource_data.update(**kwargs) - body = common.prepare_request_data(nfp_context, resource, - resource_type, resource_data, - description['service_vendor']) - return body - - def _post(self, context, tenant_id, name, nf, **kwargs): - body = self._data_wrapper(context, tenant_id, name, - 'CREATE', nf, **kwargs) - transport.send_request_to_configurator(self._conf, - context, body, "CREATE") - - def _put(self, context, tenant_id, name, nf, **kwargs): - body = self._data_wrapper(context, tenant_id, name, - 'UPDATE', nf, **kwargs) - transport.send_request_to_configurator(self._conf, - context, body, "UPDATE") - - def _delete(self, context, tenant_id, name, nf, **kwargs): - body = self._data_wrapper(context, tenant_id, name, - 'DELETE', nf, **kwargs) - transport.send_request_to_configurator(self._conf, - context, body, "DELETE") - - def _fetch_nf_from_resource_desc(self, desc): - desc_dict = ast.literal_eval(desc) - nf_id = desc_dict['network_function_id'] - return nf_id - - def _get_primary_cn(self, tls_cert): - """Returns primary CN for Certificate.""" - return cert_parser.get_host_names(tls_cert.get_certificate())['cn'] - - @staticmethod - def _get_listeners_dict_list(resource_type, resource_dict): - if resource_type.lower() == 'loadbalancer': - listeners = resource_dict['listeners'] - elif resource_type.lower() == 'listener': - listeners = [resource_dict] - elif resource_type.lower() == 'pool': - listeners = resource_dict['listeners'] - elif resource_type.lower() == 'member': - listeners = resource_dict['pool']['listeners'] - elif resource_type.lower() == 'healthmonitor': - listeners = resource_dict['pool']['listeners'] - else: - listeners = [] - - return listeners - - def _update_tls_cert(self, resource_type, resource_dict): - listeners = self._get_listeners_dict_list(resource_type, resource_dict) - for listener in listeners: - if listener['protocol'] != \ - lbv2_constants.PROTOCOL_TERMINATED_HTTPS: - continue - cert_mgr = self._cert_manager_plugin.CertManager() - lb_id = listener.get('loadbalancer_id') - tenant_id = listener.get('tenant_id') - - def get_cert(cont_id): - try: - cert_cont = cert_mgr.get_cert( - project_id=tenant_id, - cert_ref=cont_id, - resource_ref=cert_mgr.get_service_url(lb_id), - check_only=True - ) - return cert_cont - except Exception as e: - if hasattr(e, 'status_code') and e.status_code == 404: - raise loadbalancerv2.TLSContainerNotFound( - container_id=cont_id) - else: - # Could be a keystone configuration error... - raise loadbalancerv2.CertManagerError( - ref=cont_id, reason=e.message - ) - - def build_container_dict(cont_id, cert_cont): - return { - "id": cont_id, - "primary_cn": self._get_primary_cn(cert_cont), - "private_key": cert_cont.get_private_key(), - "certificate": cert_cont.get_certificate(), - "intermediates": cert_cont.get_intermediates() - } - - if not listener['default_tls_container_id']: - raise loadbalancerv2.TLSDefaultContainerNotSpecified() - else: - container_id = listener['default_tls_container_id'] - cert_container = get_cert(container_id) - container_dict = \ - build_container_dict(container_id, cert_container) - listener["default_tls_container"] = container_dict - - for container in listener.get("sni_containers"): - container_id = container["tls_container_id"] - cert_container = get_cert(container_id) - container_dict = \ - build_container_dict(container_id, cert_container) - container["tls_container"] = container_dict - - # REVISIT(jiahao): Argument allocate_vip and - # delete_vip_port are not implememnted. - @log_helpers.log_method_call - def create_loadbalancer(self, context, loadbalancer, driver_name, - allocate_vip=True): - nfp_context = module_context.init() - LOG.info("Received RPC CREATE LOADBALANCER for LB:%(lb)s", - {'lb': loadbalancer}) - # Fetch nf_id from description of the resource - nf_id = self._fetch_nf_from_resource_desc(loadbalancer["description"]) - nfp_context['log_context']['meta_id'] = nf_id - nf = common.get_network_function_details(context, nf_id) - self._update_tls_cert('loadbalancer', loadbalancer) - self._post( - context, loadbalancer['tenant_id'], - 'loadbalancer', nf, - loadbalancer=loadbalancer, driver_name=driver_name) - - @log_helpers.log_method_call - def update_loadbalancer(self, context, old_loadbalancer, loadbalancer): - nfp_context = module_context.init() - # Fetch nf_id from description of the resource - nf_id = self._fetch_nf_from_resource_desc(loadbalancer["description"]) - nfp_context['log_context']['meta_id'] = nf_id - nf = common.get_network_function_details(context, nf_id) - self._update_tls_cert('loadbalancer', loadbalancer) - self._put( - context, loadbalancer['tenant_id'], - 'loadbalancer', nf, - old_loadbalancer=old_loadbalancer, loadbalancer=loadbalancer) - - @log_helpers.log_method_call - def delete_loadbalancer(self, context, loadbalancer, - delete_vip_port=True): - nfp_context = module_context.init() - LOG.info("Received RPC DELETE LOADBALANCER for LB:" - "%(lb)s", {'lb': loadbalancer}) - # Fetch nf_id from description of the resource - nf_id = self._fetch_nf_from_resource_desc(loadbalancer["description"]) - nfp_context['log_context']['meta_id'] = nf_id - nf = common.get_network_function_details(context, nf_id) - self._update_tls_cert('loadbalancer', loadbalancer) - self._delete( - context, loadbalancer['tenant_id'], - 'loadbalancer', nf, loadbalancer=loadbalancer) - - @log_helpers.log_method_call - def create_listener(self, context, listener): - nfp_context = module_context.init() - LOG.info("Received RPC CREATE LISTENER for Listener:%(listener)s", - {'listener': listener}) - loadbalancer = listener['loadbalancer'] - # Fetch nf_id from description of the resource - nf_id = self._fetch_nf_from_resource_desc(loadbalancer["description"]) - nfp_context['log_context']['meta_id'] = nf_id - nf = common.get_network_function_details(context, nf_id) - self._update_tls_cert('listener', listener) - self._post( - context, listener['tenant_id'], - 'listener', nf, listener=listener) - - @log_helpers.log_method_call - def update_listener(self, context, old_listener, listener): - nfp_context = module_context.init() - loadbalancer = listener['loadbalancer'] - # Fetch nf_id from description of the resource - nf_id = self._fetch_nf_from_resource_desc(loadbalancer["description"]) - nfp_context['log_context']['meta_id'] = nf_id - nf = common.get_network_function_details(context, nf_id) - self._update_tls_cert('listener', listener) - self._put( - context, listener['tenant_id'], - 'listener', nf, old_listener=old_listener, listener=listener) - - @log_helpers.log_method_call - def delete_listener(self, context, listener): - nfp_context = module_context.init() - LOG.info("Received RPC DELETE LISTENER for Listener:%(listener)s", - {'listener': listener}) - loadbalancer = listener['loadbalancer'] - # Fetch nf_id from description of the resource - nf_id = self._fetch_nf_from_resource_desc(loadbalancer["description"]) - nfp_context['log_context']['meta_id'] = nf_id - nf = common.get_network_function_details(context, nf_id) - self._update_tls_cert('listener', listener) - self._delete( - context, listener['tenant_id'], - 'listener', nf, listener=listener) - - @log_helpers.log_method_call - def create_pool(self, context, pool): - nfp_context = module_context.init() - LOG.info("Received RPC CREATE POOL for Pool:%(pool)s", - {'pool': pool}) - loadbalancer = pool['loadbalancer'] - # Fetch nf_id from description of the resource - nf_id = self._fetch_nf_from_resource_desc(loadbalancer["description"]) - nfp_context['log_context']['meta_id'] = nf_id - nf = common.get_network_function_details(context, nf_id) - self._update_tls_cert('pool', pool) - self._post( - context, pool['tenant_id'], - 'pool', nf, pool=pool) - - @log_helpers.log_method_call - def update_pool(self, context, old_pool, pool): - nfp_context = module_context.init() - loadbalancer = pool['loadbalancer'] - # Fetch nf_id from description of the resource - nf_id = self._fetch_nf_from_resource_desc(loadbalancer["description"]) - nfp_context['log_context']['meta_id'] = nf_id - nf = common.get_network_function_details(context, nf_id) - self._update_tls_cert('pool', pool) - self._put( - context, pool['tenant_id'], - 'pool', nf, old_pool=old_pool, pool=pool) - - @log_helpers.log_method_call - def delete_pool(self, context, pool): - nfp_context = module_context.init() - LOG.info("Received RPC DELETE POOL for Pool:%(pool)s", - {'pool': pool}) - loadbalancer = pool['loadbalancer'] - # Fetch nf_id from description of the resource - nf_id = self._fetch_nf_from_resource_desc(loadbalancer["description"]) - nfp_context['log_context']['meta_id'] = nf_id - nf = common.get_network_function_details(context, nf_id) - self._update_tls_cert('pool', pool) - self._delete( - context, pool['tenant_id'], - 'pool', nf, pool=pool) - - @log_helpers.log_method_call - def create_member(self, context, member): - nfp_context = module_context.init() - LOG.info("Received RPC CREATE MEMBER for Member:%(member)s", - {'member': member}) - loadbalancer = member['pool']['loadbalancer'] - # Fetch nf_id from description of the resource - nf_id = self._fetch_nf_from_resource_desc(loadbalancer["description"]) - nfp_context['log_context']['meta_id'] = nf_id - nf = common.get_network_function_details(context, nf_id) - self._update_tls_cert('member', member) - self._post( - context, member['tenant_id'], - 'member', nf, member=member) - - @log_helpers.log_method_call - def update_member(self, context, old_member, member): - nfp_context = module_context.init() - loadbalancer = member['pool']['loadbalancer'] - # Fetch nf_id from description of the resource - nf_id = self._fetch_nf_from_resource_desc(loadbalancer["description"]) - nfp_context['log_context']['meta_id'] = nf_id - nf = common.get_network_function_details(context, nf_id) - self._update_tls_cert('member', member) - self._put( - context, member['tenant_id'], - 'member', nf, old_member=old_member, member=member) - - @log_helpers.log_method_call - def delete_member(self, context, member): - nfp_context = module_context.init() - LOG.info("Received RPC DELETE MEMBER for Member:%(member)s", - {'member': member}) - loadbalancer = member['pool']['loadbalancer'] - # Fetch nf_id from description of the resource - nf_id = self._fetch_nf_from_resource_desc(loadbalancer["description"]) - nfp_context['log_context']['meta_id'] = nf_id - nf = common.get_network_function_details(context, nf_id) - self._update_tls_cert('member', member) - self._delete( - context, member['tenant_id'], - 'member', nf, member=member) - - @log_helpers.log_method_call - def create_healthmonitor(self, context, healthmonitor): - nfp_context = module_context.init() - LOG.info("Received RPC CREATE HEALTH MONITOR for HM:%(hm)s", - {'hm': healthmonitor}) - loadbalancer = healthmonitor['pool']['loadbalancer'] - # Fetch nf_id from description of the resource - nf_id = self._fetch_nf_from_resource_desc(loadbalancer["description"]) - nfp_context['log_context']['meta_id'] = nf_id - nf = common.get_network_function_details(context, nf_id) - self._update_tls_cert('healthmonitor', healthmonitor) - self._post( - context, healthmonitor['tenant_id'], - 'healthmonitor', nf, healthmonitor=healthmonitor) - - @log_helpers.log_method_call - def update_healthmonitor(self, context, old_healthmonitor, healthmonitor): - nfp_context = module_context.init() - loadbalancer = healthmonitor['pool']['loadbalancer'] - # Fetch nf_id from description of the resource - nf_id = self._fetch_nf_from_resource_desc(loadbalancer["description"]) - nfp_context['log_context']['meta_id'] = nf_id - nf = common.get_network_function_details(context, nf_id) - self._update_tls_cert('healthmonitor', healthmonitor) - self._put( - context, healthmonitor['tenant_id'], - 'healthmonitor', nf, - old_healthmonitor=old_healthmonitor, healthmonitor=healthmonitor) - - @log_helpers.log_method_call - def delete_healthmonitor(self, context, healthmonitor): - nfp_context = module_context.init() - LOG.info("Received RPC DELETE HEALTH MONITOR for HM:%(hm)s", - {'hm': healthmonitor}) - loadbalancer = healthmonitor['pool']['loadbalancer'] - # Fetch nf_id from description of the resource - nf_id = self._fetch_nf_from_resource_desc(loadbalancer["description"]) - nfp_context['log_context']['meta_id'] = nf_id - nf = common.get_network_function_details(context, nf_id) - self._update_tls_cert('healthmonitor', healthmonitor) - self._delete( - context, healthmonitor['tenant_id'], - 'healthmonitor', nf, healthmonitor=healthmonitor) - - # REVISIT(jiahao): L7policy support not implemented - # disable L7policy - # def create_l7policy(self, context, l7policy): - # self._post( - # context, l7policy['tenant_id'], - # 'l7policy', l7policy=l7policy) - # - # def delete_l7policy(self, context, l7policy): - # self._delete( - # context, l7policy['tenant_id'], - # 'l7policy', l7policy=l7policy) - # - # def create_l7policy_rule(self, context, rule, l7policy_id): - # self._post( - # context, rule['tenant_id'], - # 'rule', rule=rule) - # - # def delete_l7policy_rule(self, context, rule): - # self._delete( - # context, rule['tenant_id'], - # 'rule', rule=rule) - # - # def _get_lb_context(self, context, filters): - # args = {'context': context, 'filters': filters} - # db_data = super(Lbv2Agent, self) - # return {'loadbalancers': db_data.get_loadbalancers(**args), - # 'listeners': db_data.get_listeners(**args), - # 'pools': db_data.get_pools(**args), - # 'pool_members': db_data.get_pool_members(**args), - # 'healthmonitors': db_data.get_healthmonitors(**args), - # 'l7policies': db_data.get_l7policies(**args), - # 'l7policy_rules': db_data.get_l7policy_rules(**args)} diff --git a/gbpservice/contrib/nfp/config_orchestrator/handlers/config/vpn.py b/gbpservice/contrib/nfp/config_orchestrator/handlers/config/vpn.py deleted file mode 100644 index 86930f379..000000000 --- a/gbpservice/contrib/nfp/config_orchestrator/handlers/config/vpn.py +++ /dev/null @@ -1,198 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import ast -import copy - -from gbpservice.contrib.nfp.config_orchestrator.common import common -from gbpservice.nfp.common import constants as const -from gbpservice.nfp.common import data_formatter as df -from gbpservice.nfp.common import utils -from gbpservice.nfp.core import context as module_context -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.lib import transport - -from neutron_vpnaas.db.vpn import vpn_db - -from oslo_log import helpers as log_helpers -import oslo_messaging as messaging - -LOG = nfp_logging.getLogger(__name__) - -""" -RPC handler for VPN service -""" - - -class VpnAgent(vpn_db.VPNPluginDb, vpn_db.VPNPluginRpcDbMixin): - RPC_API_VERSION = '1.0' - target = messaging.Target(version=RPC_API_VERSION) - - def __init__(self, conf, sc): - super(VpnAgent, self).__init__() - self._conf = conf - self._sc = sc - self._db_inst = super(VpnAgent, self) - - def _get_vpn_context(self, context, tenant_id, vpnservice_id, - ikepolicy_id, ipsecpolicy_id, - ipsec_site_conn_id, desc): - vpnservices = self._get_vpnservices(context, tenant_id, - vpnservice_id, desc) - ikepolicies = self._get_ikepolicies(context, tenant_id, - ikepolicy_id) - ipsecpolicies = self._get_ipsecpolicies(context, tenant_id, - ipsecpolicy_id) - ipsec_site_conns = self._get_ipsec_site_conns(context, tenant_id, - ipsec_site_conn_id, desc) - - return {'vpnservices': vpnservices, - 'ikepolicies': ikepolicies, - 'ipsecpolicies': ipsecpolicies, - 'ipsec_site_conns': ipsec_site_conns} - - def _context(self, context, tenant_id, resource, resource_data): - if context.is_admin: - tenant_id = context.tenant_id - if resource.lower() == 'ipsec_site_connection': - vpn_ctx_db = self._get_vpn_context(context, - tenant_id, - resource_data[ - 'vpnservice_id'], - resource_data[ - 'ikepolicy_id'], - resource_data[ - 'ipsecpolicy_id'], - resource_data['id'], - resource_data[ - 'description']) - return vpn_ctx_db - elif resource.lower() == 'vpn_service': - return {'vpnservices': [resource_data]} - else: - return None - - def _prepare_resource_context_dicts(self, context, tenant_id, - resource, resource_data, - context_resource_data): - # Prepare context_dict - ctx_dict = context.to_dict() - # Collecting db entry required by configurator. - # Addind service_info to neutron context and sending - # dictionary format to the configurator. - db = self._context(context, tenant_id, resource, - resource_data) - rsrc_ctx_dict = copy.deepcopy(ctx_dict) - rsrc_ctx_dict.update({'service_info': db}) - rsrc_ctx_dict.update({'resource_data': context_resource_data}) - return ctx_dict, rsrc_ctx_dict - - def _get_resource_data(self, description, resource_type): - resource_data = df.get_network_function_info(description, - resource_type) - return resource_data - - def _update_request_data(self, body, description): - pass - - def _data_wrapper(self, context, tenant_id, nf, **kwargs): - nfp_context = {} - description, str_description = ( - utils.get_vpn_description_from_nf(nf)) - description.update({'tenant_id': tenant_id}) - context_resource_data = self._get_resource_data(description, - const.VPN) - resource = kwargs['rsrc_type'] - resource_data = kwargs['resource'] - # REVISIT(dpak): We need to avoid resource description - # dependency in OTC and instead use neutron context description. - resource_data['description'] = str_description - if resource.lower() == 'ipsec_site_connection': - nfp_context = {'network_function_id': nf['id'], - 'ipsec_site_connection_id': kwargs[ - 'rsrc_id']} - - ctx_dict, rsrc_ctx_dict = self.\ - _prepare_resource_context_dicts(context, tenant_id, - resource, resource_data, - context_resource_data) - service_vm_context = utils.get_service_vm_context( - description['service_vendor']) - nfp_context.update({'neutron_context': ctx_dict, - 'service_vm_context': service_vm_context, - 'requester': 'nas_service', - 'logging_context': - module_context.get()['log_context']}) - resource_type = 'vpn' - kwargs.update({'neutron_context': rsrc_ctx_dict}) - body = common.prepare_request_data(nfp_context, resource, - resource_type, kwargs, - description['service_vendor']) - self._update_request_data(body, description) - return body - - def _fetch_nf_from_resource_desc(self, desc): - desc_dict = ast.literal_eval(desc) - nf_id = desc_dict['network_function_id'] - return nf_id - - @log_helpers.log_method_call - def vpnservice_updated(self, context, **kwargs): - nfp_context = module_context.init() - LOG.info("Received RPC VPN SERVICE UPDATED with data:%(data)s", - {'data': kwargs}) - # Fetch nf_id from description of the resource - nf_id = self._fetch_nf_from_resource_desc(kwargs[ - 'resource']['description']) - nfp_context['log_context']['meta_id'] = nf_id - nf = common.get_network_function_details(context, nf_id) - reason = kwargs['reason'] - body = self._data_wrapper(context, kwargs[ - 'resource']['tenant_id'], nf, **kwargs) - transport.send_request_to_configurator(self._conf, - context, body, - reason) - - def _proxy_subnet_cidr(self, description): - tokens = description.split(';') - return tokens[5].split('=')[1] - - def _get_vpnservices(self, context, tenant_id, vpnservice_id, desc): - filters = {'tenant_id': [tenant_id], - 'id': [vpnservice_id]} - args = {'context': context, 'filters': filters} - vpnservices = self._db_inst.get_vpnservices(**args) - for vpnservice in vpnservices: - vpnservice['description'] = desc - return vpnservices - - def _get_ikepolicies(self, context, tenant_id, ikepolicy_id): - filters = {'tenant_id': [tenant_id], - 'id': [ikepolicy_id]} - args = {'context': context, 'filters': filters} - return self._db_inst.get_ikepolicies(**args) - - def _get_ipsecpolicies(self, context, tenant_id, ipsecpolicy_id): - filters = {'tenant_id': [tenant_id], - 'id': [ipsecpolicy_id]} - args = {'context': context, 'filters': filters} - return self._db_inst.get_ipsecpolicies(**args) - - def _get_ipsec_site_conns(self, context, tenant_id, ipsec_site_conn_id, - desc): - filters = {'tenant_id': [tenant_id], - 'id': [ipsec_site_conn_id]} - args = {'context': context, 'filters': filters} - ipsec_site_conns = self._db_inst.get_ipsec_site_connections(**args) - for ipsec_site_conn in ipsec_site_conns: - ipsec_site_conn['description'] = desc - return ipsec_site_conns diff --git a/gbpservice/contrib/nfp/config_orchestrator/handlers/notification/__init__.py b/gbpservice/contrib/nfp/config_orchestrator/handlers/notification/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/config_orchestrator/handlers/notification/handler.py b/gbpservice/contrib/nfp/config_orchestrator/handlers/notification/handler.py deleted file mode 100644 index 1d822fd91..000000000 --- a/gbpservice/contrib/nfp/config_orchestrator/handlers/notification/handler.py +++ /dev/null @@ -1,235 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import sys -import traceback - -from gbpservice.contrib.nfp.config_orchestrator.common import ( - lbv2_constants as lbv2_const) -from gbpservice.contrib.nfp.config_orchestrator.common import ( - topics as a_topics) -from gbpservice.nfp.common import constants as const -from gbpservice.nfp.core import context as module_context -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.lib import transport - -import oslo_messaging as messaging - - -LOG = nfp_logging.getLogger(__name__) - - -class RpcHandler(object): - RPC_API_VERSION = '1.0' - target = messaging.Target(version=RPC_API_VERSION) - - def __init__(self, conf, sc): - super(RpcHandler, self).__init__() - self.conf = conf - self.sc = sc - - def network_function_notification(self, context, notification_data): - module_context.init() - try: - LOG.info("Received NETWORK FUNCTION NOTIFICATION:" - "%(notification)s", - {'notification': notification_data['notification']}) - if notification_data['info']['service_type'] is not None: - handler = NaasNotificationHandler(self.conf, self.sc) - handler.\ - handle_notification(context, notification_data) - except Exception as e: - exc_type, exc_value, exc_traceback = sys.exc_info() - msg = ("Generic exception (%s) while handling message (%s) : %s" - % (e, - notification_data, - traceback.format_exception(exc_type, - exc_value, - exc_traceback))) - LOG.error(msg) - - -class FirewallNotifier(object): - - def __init__(self, conf, sc): - self._sc = sc - self._conf = conf - - def set_firewall_status(self, context, notification_data): - nfp_context = module_context.init() - notification = notification_data['notification'][0] - - request_info = notification_data.get('info') - request_context = request_info.get('context') - logging_context = request_context.get('logging_context', {}) - nfp_context['log_context'] = logging_context - - resource_data = notification['data'] - firewall_id = resource_data['firewall_id'] - status = resource_data['status'] - - LOG.info("Received firewall configuration create complete API, " - "making an RPC call set firewall status for " - "firewall:%(firewall)s and status: %(status)s", - {'firewall': firewall_id, - 'status': status}) - - # RPC call to plugin to set firewall status - rpcClient = transport.RPCClient(a_topics.FW_NFP_PLUGIN_TOPIC) - rpcClient.cctxt.cast(context, 'set_firewall_status', - host=resource_data['host'], - firewall_id=firewall_id, - status=status) - - def firewall_deleted(self, context, notification_data): - nfp_context = module_context.init() - notification = notification_data['notification'][0] - - request_info = notification_data.get('info') - request_context = request_info.get('context') - logging_context = request_context.get('logging_context', {}) - nfp_context['log_context'] = logging_context - - resource_data = notification['data'] - firewall_id = resource_data['firewall_id'] - - LOG.info("Received firewall_configuration_delete_complete API, " - "making an RPC call firewall_deleted for firewall:" - "%(firewall)s ", - {'firewall': firewall_id}) - - # RPC call to plugin to update firewall deleted - rpcClient = transport.RPCClient(a_topics.FW_NFP_PLUGIN_TOPIC) - rpcClient.cctxt.cast(context, 'firewall_deleted', - host=resource_data['host'], - firewall_id=firewall_id) - - -class LoadbalancerV2Notifier(object): - - def __init__(self, conf, sc): - self._sc = sc - self._conf = conf - - def update_status(self, context, notification_data): - nfp_context = module_context.init() - notification = notification_data['notification'][0] - - request_info = notification_data.get('info') - request_context = request_info.get('context') - logging_context = request_context.get('logging_context', {}) - nfp_context['log_context'] = logging_context - - resource_data = notification['data'] - obj_type = resource_data['obj_type'] - obj_id = resource_data['obj_id'] - - rpcClient = transport.RPCClient(a_topics.LBV2_NFP_PLUGIN_TOPIC) - rpcClient.cctxt = rpcClient.client.prepare( - version=const.LOADBALANCERV2_RPC_API_VERSION) - - lb_p_status = const.ACTIVE - lb_o_status = None - obj_p_status = resource_data['provisioning_status'] - obj_o_status = resource_data['operating_status'] - - LOG.info("Received LB's update_status API. Making an " - "update_status RPC call to plugin for %(obj_type)s:" - "%(obj_id)s with status: %(status)s", - {'obj_type': obj_type, - 'obj_id': obj_id, - 'status': obj_p_status}) - - if obj_type == 'healthmonitor': - obj_o_status = None - - if obj_type != 'loadbalancer': - rpcClient.cctxt.cast(context, 'update_status', - obj_type=obj_type, - obj_id=obj_id, - provisioning_status=obj_p_status, - operating_status=obj_o_status) - else: - lb_o_status = lbv2_const.ONLINE - if obj_p_status == const.ERROR: - lb_p_status = const.ERROR - lb_o_status = lbv2_const.OFFLINE - - rpcClient.cctxt.cast(context, 'update_status', - obj_type='loadbalancer', - obj_id=resource_data['root_lb_id'], - provisioning_status=lb_p_status, - operating_status=lb_o_status) - - # TODO(jiahao): implememnt later - def update_loadbalancer_stats(self, context, loadbalancer_id, stats_data): - pass - - -class VpnNotifier(object): - - def __init__(self, conf, sc): - self._sc = sc - self._conf = conf - - def update_status(self, context, notification_data): - nfp_context = module_context.init() - resource_data = notification_data['notification'][0]['data'] - - request_info = notification_data.get('info') - request_context = request_info.get('context') - logging_context = request_context.get('logging_context', {}) - nfp_context['log_context'] = logging_context - - status = resource_data['status'] - LOG.info("Received VPN's update_status API. " - "Making an update_status RPC cast to plugin for object" - "with status: %(status)s", - {'status': status}) - rpcClient = transport.RPCClient(a_topics.VPN_NFP_PLUGIN_TOPIC) - rpcClient.cctxt.cast(context, 'update_status', - status=status) - - def ipsec_site_conn_deleted(self, context, notification_data): - pass - - -ServicetypeToHandlerMap = {'firewall': FirewallNotifier, - 'loadbalancerv2': LoadbalancerV2Notifier, - 'vpn': VpnNotifier} - - -class NaasNotificationHandler(object): - - def __init__(self, conf, sc): - self.conf = conf - self.sc = sc - - def handle_notification(self, context, notification_data): - try: - LOG.debug("Handling Notification with Data:%s", - notification_data) - resource_data = notification_data['notification'][0]['data'] - handler = ServicetypeToHandlerMap[notification_data[ - 'info']['service_type']](self.conf, self.sc) - method = getattr(handler, resource_data['notification_type']) - # Handle RPC Event - method(context, notification_data) - except Exception as e: - exc_type, exc_value, exc_traceback = sys.exc_info() - msg = ("Generic exception (%s) while handling message (%s) : %s" - % (e, - notification_data, - traceback.format_exception(exc_type, - exc_value, - exc_traceback))) - LOG.error(msg) diff --git a/gbpservice/contrib/nfp/config_orchestrator/modules/__init__.py b/gbpservice/contrib/nfp/config_orchestrator/modules/__init__.py deleted file mode 100644 index 4669116df..000000000 --- a/gbpservice/contrib/nfp/config_orchestrator/modules/__init__.py +++ /dev/null @@ -1,45 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. -from oslo_config import cfg as oslo_config - -from gbpservice.nfp.core import context -from gbpservice.nfp.orchestrator import context as module_context - -context.NfpContext = module_context.NfpContext - -openstack_opts = [ - oslo_config.StrOpt('auth_host', - default='localhost', - help='Openstack controller IP Address'), - # REVISIT: In future, use nfp_user with admin role instead of admin_user - oslo_config.StrOpt('admin_user', - help='Admin user name to create service VMs'), - oslo_config.StrOpt('admin_password', - help='Admin password to create service VMs'), - # REVISIT: In future, use nfp_tenant_name instead of admin_tenant_name - oslo_config.StrOpt('admin_tenant_name', - help='Admin tenant name to create service VMs'), - oslo_config.StrOpt('admin_tenant_id', - help='Admin tenant ID to create service VMs'), - oslo_config.StrOpt('auth_protocol', - default='http', help='Auth protocol used.'), - oslo_config.IntOpt('auth_port', - default='5000', help='Auth protocol used.'), - oslo_config.IntOpt('bind_port', - default='9696', help='Auth protocol used.'), - oslo_config.StrOpt('auth_version', - default='v2.0', help='Auth protocol used.'), - oslo_config.StrOpt('auth_uri', - default='', help='Auth URI.'), -] - -oslo_config.CONF.register_opts(openstack_opts, "nfp_keystone_authtoken") diff --git a/gbpservice/contrib/nfp/config_orchestrator/modules/config_orch.py b/gbpservice/contrib/nfp/config_orchestrator/modules/config_orch.py deleted file mode 100644 index 1255f0146..000000000 --- a/gbpservice/contrib/nfp/config_orchestrator/modules/config_orch.py +++ /dev/null @@ -1,86 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from gbpservice.contrib.nfp.config_orchestrator.common import ( - topics as a_topics) -from gbpservice.contrib.nfp.config_orchestrator.handlers.config import ( - firewall as fw) -from gbpservice.contrib.nfp.config_orchestrator.handlers.config import ( - loadbalancerv2 as lbv2) -from gbpservice.contrib.nfp.config_orchestrator.handlers.config import vpn -from gbpservice.contrib.nfp.config_orchestrator.handlers.notification import ( - handler as notif_handler) - -from gbpservice.nfp.core.rpc import RpcAgent -from oslo_config import cfg - - -def rpc_init(sc, conf): - fwrpcmgr = fw.FwAgent(conf, sc) - fwagent = RpcAgent( - sc, - host=cfg.CONF.host, - topic=a_topics.FW_NFP_CONFIGAGENT_TOPIC, - manager=fwrpcmgr - ) - - lbv2_report_state = { - 'binary': 'NCO', - 'host': cfg.CONF.host, - 'topic': a_topics.LBV2_NFP_CONFIGAGENT_TOPIC, - 'plugin_topic': a_topics.LBV2_NFP_PLUGIN_TOPIC, - 'agent_type': 'NFP Loadbalancer V2 agent', - 'configurations': {'device_drivers': ['loadbalancerv2']}, - 'start_flag': True, - 'report_interval': 10 - } - lbv2rpcmgr = lbv2.Lbv2Agent(conf, sc) - lbv2agent = RpcAgent( - sc, - host=cfg.CONF.host, - topic=a_topics.LBV2_NFP_CONFIGAGENT_TOPIC, - manager=lbv2rpcmgr, - report_state=lbv2_report_state - ) - - vpn_report_state = { - 'binary': 'NCO', - 'host': cfg.CONF.host, - 'topic': a_topics.VPN_NFP_CONFIGAGENT_TOPIC, - 'plugin_topic': a_topics.VPN_NFP_PLUGIN_TOPIC, - 'agent_type': 'NFP Vpn agent', - 'configurations': {'device_drivers': ['vpn']}, - 'start_flag': True, - 'report_interval': 10 - } - vpnrpcmgr = vpn.VpnAgent(conf, sc) - vpnagent = RpcAgent( - sc, - host=cfg.CONF.host, - topic=a_topics.VPN_NFP_CONFIGAGENT_TOPIC, - manager=vpnrpcmgr, - report_state=vpn_report_state - ) - - rpchandler = notif_handler.RpcHandler(conf, sc) - rpcagent = RpcAgent( - sc, - host=cfg.CONF.host, - topic=a_topics.CONFIG_ORCH_TOPIC, - manager=rpchandler, - ) - - sc.register_rpc_agents([fwagent, lbv2agent, vpnagent, rpcagent]) - - -def nfp_module_init(sc, conf): - rpc_init(sc, conf) diff --git a/gbpservice/contrib/nfp/configurator/__init__.py b/gbpservice/contrib/nfp/configurator/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/configurator/advanced_controller/__init__.py b/gbpservice/contrib/nfp/configurator/advanced_controller/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/configurator/advanced_controller/controller.py b/gbpservice/contrib/nfp/configurator/advanced_controller/controller.py deleted file mode 100644 index d50c127d8..000000000 --- a/gbpservice/contrib/nfp/configurator/advanced_controller/controller.py +++ /dev/null @@ -1,338 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from neutron_lib import rpc as n_rpc -from oslo_config import cfg -from oslo_log import log as logging -import oslo_messaging -import oslo_serialization.jsonutils as jsonutils -import pecan -import pika - -from gbpservice.nfp.pecan import base_controller - -LOG = logging.getLogger(__name__) -n_rpc.init(cfg.CONF) - - -class Controller(base_controller.BaseController): - """Implements all the APIs Invoked by HTTP requests. - - Implements following HTTP methods. - -get - -post - -put - According to the HTTP request received from config-agent this class make - call/cast to configurator and return response to config-agent - - """ - - def __init__(self, method_name): - try: - self.method_name = method_name - self.services = pecan.conf['cloud_services'] - self.rpc_routing_table = {} - for service in self.services: - self._entry_to_rpc_routing_table(service) - - configurator_notifications = self.services[0]['notifications'] - self.rmqconsumer = RMQConsumer(configurator_notifications['host'], - configurator_notifications['queue'] - ) - super(Controller, self).__init__() - except Exception as err: - msg = ( - "Failed to initialize Controller class %s." % - str(err).capitalize()) - LOG.error(msg) - - def _entry_to_rpc_routing_table(self, service): - """Prepares routing table based on the uservice configuration. - This routing table is used to route the rpcs to all interested - uservices. Key used for routing is the uservice[apis]. - - :param uservice - e.g uservice = {'service_name': 'configurator', - 'topic': 'configurator', - 'reporting_interval': '10', # in seconds - 'apis': ['CONFIGURATION', 'EVENT'] - } - Returns: None - - Prepares: self.rpc_routing_table - e.g self.rpc_routing_table = {'CONFIGURATION': [rpc_client, ...], - 'EVENT': [rpc_client, ...] - } - """ - for api in service['apis']: - if api not in self.rpc_routing_table: - self.rpc_routing_table[api] = [] - - self.rpc_routing_table[api].append(CloudService(**service)) - - @pecan.expose(method='GET', content_type='application/json') - def get(self): - """Method of REST server to handle request get_notifications. - - This method send an RPC call to configurator and returns Notification - data to config-agent - - Returns: Dictionary that contains Notification data - - """ - - try: - if self.method_name == 'get_notifications': - notification_data = self.rmqconsumer.pull_notifications() - msg = ("NOTIFICATION_DATA sent to config_agent %s" - % notification_data) - LOG.info(msg) - return jsonutils.dumps(notification_data) - - except Exception as err: - pecan.response.status = 400 - msg = ("Failed to handle request=%s. Reason=%s." - % (self.method_name, str(err).capitalize())) - LOG.error(msg) - error_data = self._format_description(msg) - return jsonutils.dumps(error_data) - - @pecan.expose(method='POST', content_type='application/json') - def post(self, **body): - """Method of REST server to handle all the post requests. - - This method sends an RPC cast to configurator according to the - HTTP request. - - :param body: This method excepts dictionary as a parameter in HTTP - request and send this dictionary to configurator with RPC cast. - - Returns: None - - """ - - try: - body = None - if pecan.request.is_body_readable: - body = pecan.request.json_body - - routing_key = body.pop("routing_key", "CONFIGURATION") - for uservice in self.rpc_routing_table[routing_key]: - uservice.rpcclient.cast(self.method_name, body) - msg = ('Sent RPC to %s' % (uservice.topic)) - LOG.info(msg) - - msg = ("Successfully served HTTP request %s" % self.method_name) - LOG.info(msg) - - except Exception as err: - pecan.response.status = 400 - msg = ("Failed to serve HTTP post request %s %s." - % (self.method_name, str(err).capitalize())) - # extra_import = ("need to remove this import %s" % config) - # LOG.debug(extra_import) - LOG.error(msg) - error_data = self._format_description(msg) - return jsonutils.dumps(error_data) - - @pecan.expose(method='PUT', content_type='application/json') - def put(self, **body): - """Method of REST server to handle all the put requests. - - This method sends an RPC cast to configurator according to the - HTTP request. - - :param body: This method excepts dictionary as a parameter in HTTP - request and send this dictionary to configurator with RPC cast. - - Returns: None - - """ - try: - body = None - if pecan.request.is_body_readable: - body = pecan.request.json_body - - routing_key = body.pop("routing_key", "CONFIGURATION") - for uservice in self.rpc_routing_table[routing_key]: - uservice.rpcclient.cast(self.method_name, body) - msg = ('Sent RPC to %s' % (uservice.topic)) - LOG.info(msg) - msg = ("Successfully served HTTP request %s" % self.method_name) - LOG.info(msg) - - except Exception as err: - pecan.response.status = 400 - msg = ("Failed to serve HTTP put request %s %s." - % (self.method_name, str(err).capitalize())) - LOG.error(msg) - error_data = self._format_description(msg) - return jsonutils.dumps(error_data) - - def _format_description(self, msg): - """This methgod formats error description. - - :param msg: An error message that is to be formatted - - Returns: error_data dictionary - """ - - error_data = {'failure_desc': {'msg': msg}} - return error_data - - -class RPCClient(object): - """Implements call/cast methods used in REST Controller. - - Implements following methods. - -call - -cast - This class send an RPC call/cast to configurator according to the data sent - by Controller class of REST server. - - """ - - API_VERSION = '1.0' - - def __init__(self, topic): - - self.topic = topic - target = oslo_messaging.Target( - topic=self.topic, - version=self.API_VERSION) - self.client = n_rpc.get_client(target) - - def call(self, method_name): - """Method for sending call request on behalf of REST Controller. - - This method sends an RPC call to configurator. - - Returns: Notification data sent by configurator. - - """ - cctxt = self.client.prepare(version=self.API_VERSION, - topic=self.topic) - return cctxt.call(self, method_name) - - def cast(self, method_name, request_data): - """Method for sending cast request on behalf of REST Controller. - - This method sends an RPC cast to configurator according to the - method_name passed by COntroller class of REST server. - - :param method_name:method name can be any of the following. - - - Returns: None. - - """ - cctxt = self.client.prepare(version=self.API_VERSION, - topic=self.topic) - - return cctxt.cast(self, - method_name, - request_data=request_data) - - def to_dict(self): - """This function return empty dictionary. - - For making RPC call/cast it internally requires context class that - contains to_dict() function. Here we are sending context inside - request data so we are passing class itself as a context that - contains to_dict() function. - - Returns: Dictionary. - - """ - return {} - - -class CloudService(object): - """ CloudService keeps all information of uservice along with initialized - RPCClient object using which rpc is routed to over the cloud service. - """ - - def __init__(self, **kwargs): - self.service_name = kwargs.get('service_name') - self.topic = kwargs.get('topic') - self.reporting_interval = kwargs.get('reporting_interval') - self.rpcclient = RPCClient(topic=self.topic) - - -class RMQConsumer(object): - """RMQConsumer for over the cloud services. - - This class access rabbitmq's 'configurator-notifications' queue - to pull all the notifications came from over the cloud services. - - """ - - def __init__(self, rabbitmq_host, queue): - self.rabbitmq_host = rabbitmq_host - self.queue = queue - self.create_connection() - - def create_connection(self): - try: - self.connection = pika.BlockingConnection( - pika.ConnectionParameters - (host=self.rabbitmq_host)) - except Exception as e: - msg = ("Failed to create rmq connection %s" % (e)) - LOG.error(msg) - - def _fetch_data_from_wrapper_strct(self, oslo_notifications): - notifications = [] - for oslo_notification_data in oslo_notifications: - notification_data = jsonutils.loads( - oslo_notification_data["oslo.message"] - )["args"]["notification_data"] - notifications.extend(notification_data) - return notifications - - def pull_notifications(self): - notifications = [] - msgs_acknowledged = False - try: - self.channel = self.connection.channel() - self.queue_declared = self.channel.queue_declare(queue=self.queue, - durable=True) - self.channel.queue_bind(self.queue, 'openstack') - pending_msg_count = self.queue_declared.method.message_count - log = ('[notifications queue:%s, pending notifications:%s]' - % (self.queue, pending_msg_count)) - LOG.info(log) - for i in range(pending_msg_count): - method, properties, body = self.channel.basic_get(self.queue) - notifications.append(jsonutils.loads(body)) - - # Acknowledge all messages delivery - if pending_msg_count > 0: - self.channel.basic_ack(delivery_tag=method.delivery_tag, - multiple=True) - msgs_acknowledged = True - - self.channel.close() - return self._fetch_data_from_wrapper_strct(notifications) - - except pika.exceptions.ConnectionClosed: - msg = ("Caught ConnectionClosed exception.Creating new connection") - LOG.error(msg) - self.create_connection() - return self._fetch_data_from_wrapper_strct(notifications) - except pika.exceptions.ChannelClosed: - msg = ("Caught ChannelClosed exception.") - LOG.error(msg) - if msgs_acknowledged is False: - return self.pull_notifications() - else: - return self._fetch_data_from_wrapper_strct(notifications) diff --git a/gbpservice/contrib/nfp/configurator/advanced_controller/controller_loader.py b/gbpservice/contrib/nfp/configurator/advanced_controller/controller_loader.py deleted file mode 100644 index 9831524e2..000000000 --- a/gbpservice/contrib/nfp/configurator/advanced_controller/controller_loader.py +++ /dev/null @@ -1,64 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import pecan - -from gbpservice.contrib.nfp.configurator.advanced_controller import ( - controller) - -"""This class forwards HTTP request to controller class. - -This class create an object of Controller class with appropriate -parameter according to the path of HTTP request. According to the -parameter passed to Controller class it sends an RPC call/cast to -configurator. - -""" - - -class ControllerResolver(object): - - create_network_function_device_config = controller.Controller( - "create_network_function_device_config") - delete_network_function_device_config = controller.Controller( - "delete_network_function_device_config") - update_network_function_device_config = controller.Controller( - "update_network_function_device_config") - create_network_function_config = controller.Controller( - "create_network_function_config") - delete_network_function_config = controller.Controller( - "delete_network_function_config") - update_network_function_config = controller.Controller( - "update_network_function_config") - get_notifications = controller.Controller("get_notifications") - network_function_event = controller.Controller("network_function_event") - get_requests = controller.Controller("get_requests") - - -""" This class forwards HTTP requests starting with /v1/nfp. - -All HTTP requests with path starting from /v1 -land here. This class forward request with path starting from /v1/nfp -to ControllerResolver. - -""" - - -class V1Controller(object): - - nfp = ControllerResolver() - - @pecan.expose() - def get(self): - return {'versions': [{'status': 'CURRENT', - 'updated': '2014-12-11T00:00:00Z', - 'id': 'v1'}]} diff --git a/gbpservice/contrib/nfp/configurator/agents/__init__.py b/gbpservice/contrib/nfp/configurator/agents/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/configurator/agents/agent_base.py b/gbpservice/contrib/nfp/configurator/agents/agent_base.py deleted file mode 100644 index 1ca19665e..000000000 --- a/gbpservice/contrib/nfp/configurator/agents/agent_base.py +++ /dev/null @@ -1,280 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from neutron_lib import rpc as n_rpc -from oslo_config import cfg -import oslo_messaging as messaging - -from gbpservice.contrib.nfp.configurator.lib import constants as const -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.core import module as nfp_api - -n_rpc.init(cfg.CONF) - -LOG = nfp_logging.getLogger(__name__) - - -class AgentBaseRPCManager(object): - """Implements base class for all service agents. - - Common methods for service agents are implemented in this class. - Configurator module invokes these methods through the service - agent's child class instance. - - """ - - def __init__(self, sc, conf): - self.sc = sc - self.conf = conf - - def validate_request(self, sa_req_list, notification_data): - """Preliminary validation of function input. - - :param sa_req_list: List of data blobs prepared by de-multiplexer - for service agents processing. - :param notification_data: Notification blobs prepared by the service - agents after processing requests blobs. Each request blob will have - a corresponding notification blob. - - Returns: True if validation passes. False if validation fails. - - """ - - if (isinstance(sa_req_list, list) and - isinstance(notification_data, dict)): - return True - else: - return False - - def get_diff_of_dict(self, old_dict, new_dict): - """Getting difference between two dict. - - :param Two dictionary - - Returns: Two dictionary which has different values for same keys. - - """ - diff_values = [] - new_val = {} - old_val = {} - for key in new_dict: - if old_dict.get(key) != new_dict.get(key): - diff_values.append(key) - - for value in diff_values: - if value == 'description': - pass - else: - new_val[value] = new_dict.get(value) - old_val[value] = old_dict.get(value) - return old_val, new_val - - def process_request(self, sa_req_list, notification_data): - """Forwards the RPC message from configurator to service agents. - - Checks if the request message contains multiple data blobs. If multiple - data blobs are found, a batch event is generated otherwise a single - event. - - :param sa_req_list: List of data blobs prepared by de-multiplexer - for service agents processing. - :param notification_data: Notification blobs prepared by the service - agents after processing requests blobs. Each request blob will have - a corresponding notification blob. - - Returns: None - - """ - - # In case of malformed input, send failure notification - if not self.validate_request(sa_req_list, notification_data): - # REVISIT(JAGADISH): Need to send failure notification - return - - # Multiple request data blobs needs batch processing. Send batch - # processing event or do direct processing of single request data blob - if (len(sa_req_list) > 1): - LOG.info("Creating event PROCESS BATCH") - args_dict = { - 'sa_req_list': sa_req_list, - 'notification_data': notification_data - } - ev = self.sc.new_event(id=const.EVENT_PROCESS_BATCH, - data=args_dict, key=None) - self.sc.post_event(ev) - else: - agent_info = sa_req_list[0]['agent_info'] - # Renaming the neutron context in resource data of *aaS to context. - # Adding agent_info which contains information required for - # demux and response data in agent to neutron_context in *aaS - if not sa_req_list[0]['is_generic_config'] and not ( - agent_info['resource'] in const.NFP_SERVICE_LIST): - # Here, the neutron context is overloaded with agent_info - # dict which contains the API context in addition to other - # fields like service type, service vendor, resource etc. - # The agent_info dict is constructed inside the demuxer library - sa_req_list[0]['resource_data']['neutron_context'].update( - {'agent_info': agent_info}) - # When calling the *aaS or NFPService agents, the - # "neutron context" passed inside the resource data is - # renamed to "context" - sa_req_list[0]['resource_data']['context'] = sa_req_list[0][ - 'resource_data'].pop('neutron_context') - getattr(self, sa_req_list[0]['method'])( - **sa_req_list[0]['resource_data']) - else: - sa_req_list[0]['agent_info'].update( - {'notification_data': notification_data}) - getattr(self, sa_req_list[0]['method'])( - agent_info, sa_req_list[0]['resource_data']) - - -class AgentBaseNotification(object): - """Enqueues notification event into notification queue - - Responses from the REST calls made to the VM are fed to under the - cloud components using this notification handle. - """ - - API_VERSION = '1.0' - - def __init__(self, sc): - self.sc = sc - self.topic = const.NOTIFICATION_QUEUE - target = messaging.Target(topic=self.topic, - version=self.API_VERSION) - self.client = n_rpc.get_client(target) - self.cctxt = self.client.prepare(version=self.API_VERSION, - topic=self.topic) - - def _notification(self, data): - """Enqueues notification event into const.NOTIFICATION_QUEUE - - These events are enqueued into notification queue and are retrieved - when get_notifications() API lands on configurator. - - :param data: Event data blob - - Returns: None - - """ - self.cctxt.cast(self, 'send_notification', notification_data=[data]) - - def to_dict(self): - return {} - - -class AgentBaseEventHandler(nfp_api.NfpEventHandler): - """ Super class for all agents to handle batch events. - - """ - - def __init__(self, sc, drivers, rpcmgr): - self.sc = sc - self.drivers = drivers - self.rpcmgr = rpcmgr - self.notify = AgentBaseNotification(self.sc) - - def process_batch(self, ev): - """Processes a request with multiple data blobs. - - Configurator processes the request with multiple data blobs and sends - a list of service information to be processed. This function goes - through the list of service information and invokes specific service - driver methods. After processing each request data blob, notification - data blob is prepared. - - :param ev: Event instance that contains information of event type and - corresponding event data to be processed. - - """ - - # Get service agent information list and notification data list - # from the event data - sa_req_list = ev.data.get('sa_req_list') - notification_data = ev.data.get('notification_data') - - for request in sa_req_list: - try: - # Process the first data blob from the request list. - # Get necessary parameters needed for driver method invocation. - method = request['method'] - is_generic_config = request['is_generic_config'] - resource_data = request['resource_data'] - agent_info = request['agent_info'] - resource = agent_info['resource'] - # agent_info contains the API context. - context = agent_info['context'] - service_vendor = agent_info['service_vendor'] - service_type = agent_info['resource_type'] - service_feature = agent_info['service_feature'] - if not is_generic_config: - sa_req_list[0]['resource_data']['context'] = sa_req_list[ - 0]['resource_data'].pop('neutron_context') - - # Get the service driver and invoke its method - driver = self._get_driver(service_type, service_vendor, - service_feature) - - # Service driver should return "success" on successful API - # processing. All other return values and exceptions are - # treated as failures. - if is_generic_config: - result = getattr(driver, method)(context, resource_data) - else: - result = getattr(driver, method)(**resource_data) - success = True if result == 'SUCCESS' else False - except Exception as err: - result = ("Failed to process %s request. %s" % - (method, str(err).capitalize())) - success = False - finally: - # Prepare success notification and populate notification - # data list - if result in const.SUCCESS: - data = {'status_code': const.SUCCESS} - else: - data = {'status_code': const.FAILURE, - 'error_msg': result} - - msg = {'info': {'service_type': service_type, - 'context': context}, - 'notification': [{'resource': resource, - 'data': data}] - } - # If the data processed is first one, then prepare notification - # dict. Otherwise, append the notification to the kwargs list. - # Whether it is a data batch or single data blob request, - # notification generated will be single dictionary. In case of - # batch, multiple notifications are sent in the kwargs list. - if not notification_data: - notification_data.update(msg) - else: - data = {'resource': resource, - 'data': data} - notification_data['notification'].append(data) - - if not success: - self.notify._notification(notification_data) - raise Exception(msg) - - self.notify._notification(notification_data) - - -def init_agent_complete(cm, sc, conf): - """Placeholder method to satisfy configurator module agent loading.""" - pass - - -def init_agent(cm, sc, conf): - """Placeholder method to satisfy configurator module agent loading.""" - pass diff --git a/gbpservice/contrib/nfp/configurator/agents/firewall.py b/gbpservice/contrib/nfp/configurator/agents/firewall.py deleted file mode 100644 index 29e002aec..000000000 --- a/gbpservice/contrib/nfp/configurator/agents/firewall.py +++ /dev/null @@ -1,510 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import operator -import os - -import oslo_messaging as messaging -from oslo_serialization import jsonutils -import requests -import six - -from gbpservice.contrib.nfp.configurator.agents import agent_base -from gbpservice.contrib.nfp.configurator.lib import constants as common_const -from gbpservice.contrib.nfp.configurator.lib import fw_constants as const -from gbpservice.contrib.nfp.configurator.lib import utils as load_driver -from gbpservice.nfp.core import event as nfp_event -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.core import module as nfp_api - -LOG = nfp_logging.getLogger(__name__) - - -class FwaasRpcSender(agent_base.AgentBaseEventHandler): - """ Implements Fwaas response path to Neutron plugin. - - Methods of this class are invoked by the FwaasEventHandler class - for sending response from driver to the Fwaas Neutron plugin. - - """ - - def __init__(self, sc, host, drivers, rpcmgr): - super(FwaasRpcSender, self).__init__(sc, drivers, rpcmgr) - self.host = host - - def set_firewall_status(self, agent_info, - firewall_id, status, firewall=None): - """ Enqueues the response from FwaaS operation to neutron plugin. - - :param context: Neutron context - :param firewall_id: id of firewall resource - :param status: ACTIVE/ ERROR - - """ - - msg = {'info': {'service_type': const.SERVICE_TYPE, - 'context': agent_info['context']}, - 'notification': [{ - 'resource': agent_info['resource'], - 'data': {'firewall_id': firewall_id, - 'host': self.host, - 'status': status, - 'notification_type': ( - 'set_firewall_status')}}] - } - LOG.info("Sending Notification 'Set Firewall Status' to " - "Orchestrator for firewall: %(fw_id)s with status:" - "%(status)s", - {'fw_id': firewall_id, - 'status': status}) - self.notify._notification(msg) - - def firewall_deleted(self, agent_info, firewall_id, firewall=None): - """ Enqueues the response from FwaaS operation to neutron plugin. - - :param context: Neutron context - :param firewall_id: id of firewall resource - - """ - - msg = {'info': {'service_type': const.SERVICE_TYPE, - 'context': agent_info['context']}, - 'notification': [{ - 'resource': agent_info['resource'], - 'data': {'firewall_id': firewall_id, - 'host': self.host, - 'notification_type': ( - 'firewall_deleted')}}] - } - LOG.info("Sending Notification 'Firewall Deleted' to " - "Orchestrator for firewall: %(fw_id)s ", - {'fw_id': firewall_id}) - self.notify._notification(msg) - - -class FWaasRpcManager(agent_base.AgentBaseRPCManager): - """ Implements FWaasRpcManager class which receives requests - from Configurator to Agent. - - Methods of this class are invoked by the configurator. Events are - created according to the requests received and enqueued to worker queues. - - """ - - RPC_API_VERSION = '1.0' - target = messaging.Target(version=RPC_API_VERSION) - - def __init__(self, sc, conf): - """Instantiates child and parent class objects. - - :param sc: Service Controller object that is used to communicate - with process model core file. - :param conf: Configuration object that is used for configuration - parameter access. - - """ - - super(FWaasRpcManager, self).__init__(sc, conf) - - def _create_event(self, context, firewall, host, method): - """ Creates and enqueues the events to the worker queues. - - :param context: Neutron context - :param firewall: Firewall resource object from neutron fwaas plugin - :param host: Name of the host machine - :param method: CREATE_FIREWALL/UPDATE_FIREWALL/DELETE_FIREWALL - - """ - - # To solve the huge data issue with firewalls, - # especially with 250 firewall rule test which - # gets multipled with each consumer in the chain. - # Even the zipped data is huge and cannot be sent - # over pipe. Writing it to file here and event handler - # will read it from file and process further. - - filename = "/tmp/" + firewall['id'] - with open(filename, 'w') as f: - f.write(jsonutils.dumps(firewall)) - - arg_dict = {'context': context, - 'firewall': {'file_path': filename}, - 'host': host} - # REVISIT(mak): How to send large data ? - # New API required to send over unix sockert ? - context['service_info'] = {} - - # ev = self.sc.new_event(id=method, data={}, key=None) - ev = self.sc.new_event(id=method, data=arg_dict, key=None) - self.sc.post_event(ev) - - def create_firewall(self, context, firewall, host): - """ Receives request to create firewall from configurator - - """ - - LOG.info("Received request 'Create Firewall'.") - self._create_event(context, firewall, - host, const.FIREWALL_CREATE_EVENT) - - def update_firewall(self, context, firewall, host): - """ Receives request to update firewall from configurator - - """ - LOG.info("Received request 'Update Firewall'.") - self._create_event(context, firewall, - host, const.FIREWALL_UPDATE_EVENT) - - def delete_firewall(self, context, firewall, host): - """ Receives request to delete firewall from configurator - - """ - LOG.info("Received request 'Delete Firewall'.") - self._create_event(context, firewall, - host, const.FIREWALL_DELETE_EVENT) - - -class FWaasEventHandler(nfp_api.NfpEventHandler): - """ Handler class which invokes firewall driver methods - - Worker processes dequeue the worker queues and invokes the - appropriate handler class methods for Fwaas methods. - - """ - - def __init__(self, sc, drivers, rpcmgr, conf): - """ Instantiates class object. - - :param sc: Service Controller object that is used to communicate - with process model core file. - :param drivers: dictionary of driver name to object mapping - :param rpcmgr: FwaasRpcManager class object - - """ - - self.sc = sc - self.conf = conf - self.drivers = drivers - self.host = self.conf.host - self.rpcmgr = rpcmgr - self.plugin_rpc = FwaasRpcSender(sc, self.host, - self.drivers, self.rpcmgr) - - def _get_driver(self, service_vendor, service_feature): - """ Retrieves driver object given the service type - - """ - - driver_id = const.SERVICE_TYPE + service_vendor + service_feature - return self.drivers[driver_id] - - def _is_firewall_rule_exists(self, fw): - """ Checks if firewall rules are present in the request data - - :param fw: Firewall resource object - - """ - - if not fw['firewall_rule_list']: - return False - else: - return True - - def handle_event(self, ev): - """ Demultiplexes the firewall request to appropriate - driver methods. - - :param ev: event object sent from process model event handler - - """ - - try: - msg = ("Handling event %s" % (ev.id)) - LOG.info(msg) - - # The context here in ev.data is the neutron context that was - # renamed to context in the agent_base. This erstwhile - # neutron context contains the agent info which in turn contains - # the API context alongside other relevant information like - # service vendor and type. Agent info is constructed inside - # the demuxer library. - - if ev.data['firewall'].get('file_path', None): - filename = ev.data['firewall']['file_path'] - string = str() - with open(filename, 'r') as f: - string = f.read() - ev.data['firewall'] = jsonutils.loads(string) - try: - os.remove(filename) - except Exception as e: - msg = ("Exception while removing the file %r, " - "with error: %r" % (filename, e)) - LOG.error(msg) - - agent_info = ev.data['context']['agent_info'] - service_vendor = agent_info['service_vendor'] - service_feature = agent_info.get('service_feature', '') - driver = self._get_driver(service_vendor, service_feature) - LOG.info("Invoking driver with service vendor:" - "%(service_vendor)s ", - {'service_vendor': service_vendor}) - self.method = getattr(driver, "%s" % (ev.id.lower())) - self.invoke_driver_for_plugin_api(ev) - msg = ("Handled event %s successfully" % (ev.id)) - LOG.info(msg) - except Exception as err: - msg = ("Failed handling event: %s. Reason %s" - % (ev.id, str(err).capitalize())) - LOG.error(msg) - - def _remove_duplicate_fw_rules(self, rules): - """ Removes duplicate rules from the rules list. """ - # 'description' filter field needs to be added if required - filter_keys = ['action', 'destination_ip_address', 'destination_port', - 'enabled', 'ip_version', 'protocol', - 'source_ip_address', 'source_port', 'shared'] - filter_rules = [] - for rule in rules: - filter_rules.append({k: rule[k] for k in filter_keys}) - - unique_rules = [dict(tupleized) for tupleized in set( - tuple(rule.items()) for rule in filter_rules)] - result = [] - for d1 in unique_rules: - for d2 in rules: - if d1.items() <= d2.items(): - result.append(d2) - break - result.sort(key=operator.itemgetter('position')) - for index, x in enumerate(result): - x['position'] = index + 1 - return result - - def invoke_driver_for_plugin_api(self, ev): - """ Invokes the appropriate driver methods - - :param ev: event object sent from process model event handler - - """ - - context = ev.data['context'] - agent_info = context.get('agent_info') - firewall = ev.data.get('firewall') - host = ev.data.get('host') - firewall['firewall_rule_list'] = self._remove_duplicate_fw_rules( - firewall['firewall_rule_list']) - - if ev.id == const.FIREWALL_CREATE_EVENT: - if not self._is_firewall_rule_exists(firewall): - msg = ("Firewall rule list is empty, setting Firewall " - "status to ACTIVE %s" % (firewall)) - LOG.info(msg) - return self.plugin_rpc.set_firewall_status( - agent_info, firewall['id'], - common_const.STATUS_ACTIVE, firewall) - # Added to handle in service vm agents. VM agent will add - # default DROP rule. - # if not self._is_firewall_rule_exists(firewall): - # self.plugin_rpc.set_firewall_status( - # context, firewall['id'], const.STATUS_ACTIVE) - try: - status = self.method(context, firewall, host) - except Exception as err: - self.plugin_rpc.set_firewall_status( - agent_info, firewall['id'], common_const.STATUS_ERROR) - msg = ("Failed to configure Firewall and status is " - "changed to ERROR. %s." % str(err).capitalize()) - LOG.error(msg) - else: - self.plugin_rpc.set_firewall_status( - agent_info, firewall['id'], status, firewall) - msg = ("Configured Firewall and status set to %s" % status) - LOG.info(msg) - - elif ev.id == const.FIREWALL_DELETE_EVENT: - if not self._is_firewall_rule_exists(firewall): - msg = ("Firewall rule list is empty, sending firewall deleted " - "status to plugin %s" % (firewall)) - LOG.info(msg) - return self.plugin_rpc.firewall_deleted( - agent_info, firewall['id'], firewall) - try: - status = self.method(context, firewall, host) - except requests.ConnectionError: - # REVISIT(VIKASH): It can't be correct everytime - msg = ("There is a connection error for firewall %r of " - "tenant %r. Assuming either there is serious " - "issue with VM or data path is completely " - "broken. For now marking that as delete." - % (firewall['id'], firewall['tenant_id'])) - LOG.warning(msg) - self.plugin_rpc.firewall_deleted( - agent_info, firewall['id'], firewall) - - except Exception as err: - # REVISIT(VIKASH): Is it correct to raise ? As the subsequent - # attempt to clean will only re-raise the last one.And it - # can go on and on and may not be ever recovered. - self.plugin_rpc.set_firewall_status( - agent_info, firewall['id'], common_const.STATUS_ERROR) - msg = ("Failed to delete Firewall and status is " - "changed to ERROR. %s." % str(err).capitalize()) - LOG.error(msg) - # raise(err) - else: - if status == common_const.STATUS_ERROR: - self.plugin_rpc.set_firewall_status( - agent_info, firewall['id'], status) - else: - msg = ("Firewall %r deleted of tenant: %r" % ( - firewall['id'], firewall['tenant_id'])) - LOG.info(msg) - self.plugin_rpc.firewall_deleted( - agent_info, firewall['id'], firewall) - - elif ev.id == const.FIREWALL_UPDATE_EVENT: - if not self._is_firewall_rule_exists(firewall): - return self.plugin_rpc.set_firewall_status( - agent_info, - common_const.STATUS_ACTIVE, firewall) - try: - status = self.method(context, firewall, host) - except Exception as err: - self.plugin_rpc.set_firewall_status( - agent_info, firewall['id'], common_const.STATUS_ERROR) - msg = ("Failed to update Firewall and status is " - "changed to ERROR. %s." % str(err).capitalize()) - LOG.error(msg) - else: - self.plugin_rpc.set_firewall_status( - agent_info, firewall['id'], status, firewall) - msg = ("Updated Firewall and status set to %s" % status) - LOG.info(msg) - else: - msg = ("Wrong call to Fwaas event handler.") - raise Exception(msg) - - -def events_init(sc, drivers, rpcmgr, conf): - """Registers events with core service controller. - - All the events will come to handle_event method of class instance - registered in 'handler' field. - - :param drivers: Driver instances registered with the service agent - :param rpcmgr: Instance to receive all the RPC messages from configurator - module. - - Returns: None - - """ - - event_id_list = [const.FIREWALL_CREATE_EVENT, - const.FIREWALL_UPDATE_EVENT, - const.FIREWALL_DELETE_EVENT] - evs = [] - for event in event_id_list: - evs.append(nfp_event.Event(id=event, handler=FWaasEventHandler( - sc, drivers, rpcmgr, conf))) - sc.register_events(evs) - - -def load_drivers(conf): - """Imports all the driver files corresponding to this agent. - - Returns: Dictionary of driver objects with a specified service type and - vendor name - - """ - - ld = load_driver.ConfiguratorUtils(conf) - drivers = ld.load_drivers(const.SERVICE_TYPE) - - for service_type, driver_name in six.iteritems(drivers): - driver_obj = driver_name(conf=conf) - drivers[service_type] = driver_obj - - LOG.info("Firewall loaded drivers:%(drivers)s", - {'drivers': drivers}) - return drivers - - -def register_service_agent(cm, sc, conf, rpcmgr): - """Registers Fwaas service agent with configurator module. - - :param cm: Instance of configurator module - :param sc: Instance of core service controller - :param conf: Instance of oslo configuration - :param rpcmgr: Instance containing RPC methods which are invoked by - configurator module on corresponding RPC message arrival - - """ - - service_type = const.SERVICE_TYPE - cm.register_service_agent(service_type, rpcmgr) - - -def init_agent(cm, sc, conf): - """Initializes Fwaas agent. - - :param cm: Instance of configuration module - :param sc: Instance of core service controller - :param conf: Instance of oslo configuration - - """ - - try: - drivers = load_drivers(conf) - except Exception as err: - msg = ("Fwaas failed to load drivers. %s" - % (str(err).capitalize())) - LOG.error(msg) - raise Exception(err) - else: - msg = ("Fwaas loaded drivers successfully.") - LOG.debug(msg) - - rpcmgr = FWaasRpcManager(sc, conf) - try: - events_init(sc, drivers, rpcmgr, conf) - except Exception as err: - msg = ("Fwaas Events initialization unsuccessful. %s" - % (str(err).capitalize())) - LOG.error(msg) - raise Exception(err) - else: - msg = ("Fwaas Events initialization successful.") - LOG.debug(msg) - - try: - register_service_agent(cm, sc, conf, rpcmgr) - except Exception as err: - msg = ("Fwaas service agent registration unsuccessful. %s" - % (str(err).capitalize())) - LOG.error(msg) - raise Exception(err) - else: - msg = ("Fwaas service agent registration successful.") - LOG.debug(msg) - - msg = ("FIREWALL as a Service Module Initialized.") - LOG.info(msg) - - -def init_agent_complete(cm, sc, conf): - """ Initializes periodic tasks - - """ - - msg = (" Firewall agent init complete") - LOG.info(msg) diff --git a/gbpservice/contrib/nfp/configurator/agents/generic_config.py b/gbpservice/contrib/nfp/configurator/agents/generic_config.py deleted file mode 100644 index 69e7e544e..000000000 --- a/gbpservice/contrib/nfp/configurator/agents/generic_config.py +++ /dev/null @@ -1,546 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import copy -import os - -import six - -from gbpservice.contrib.nfp.configurator.agents import agent_base -from gbpservice.contrib.nfp.configurator.lib import ( - generic_config_constants as gen_cfg_const) -from gbpservice.contrib.nfp.configurator.lib import constants as common_const -from gbpservice.contrib.nfp.configurator.lib import data_parser -from gbpservice.contrib.nfp.configurator.lib import utils -from gbpservice.nfp.core import event as nfp_event -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.core import module as nfp_api - -LOG = nfp_logging.getLogger(__name__) - - -class GenericConfigRpcManager(agent_base.AgentBaseRPCManager): - """Implements APIs invoked by configurator for processing RPC messages. - - RPC client of configurator module receives RPC messages from REST server - and invokes the API of this class. The instance of this class is registered - with configurator module using register_service_agent API. Configurator - module identifies the service agent object based on service type and - invokes ones of the methods of this class to configure the device. - - """ - - def __init__(self, sc, conf): - """Instantiates child and parent class objects. - - Passes the instances of core service controller and oslo configuration - to parent instance inorder to provide event enqueue facility for batch - processing event. - - :param sc: Service Controller object that is used for interfacing - with core service controller. - :param conf: Configuration object that is used for configuration - parameter access. - - """ - - self.parse = data_parser.DataParser() - super(GenericConfigRpcManager, self).__init__(sc, conf) - - def _send_event(self, context, resource_data, event_id, event_key=None): - """Posts an event to framework. - - :param context: The agent info dictionary prepared in demuxer library - which contains the API context alongside other information. - :param kwargs: Keyword arguments which are passed as data to event - :param event_id: Unique identifier for the event - :param event_key: Event key for serialization - - """ - - arg_dict = {'context': context, - 'resource_data': resource_data} - ev = self.sc.new_event(id=event_id, data=arg_dict, key=event_key) - self.sc.post_event(ev) - - def configure_interfaces(self, context, resource_data): - """Enqueues event for worker to process configure interfaces request. - - :param context: The agent info dictionary prepared in demuxer library - which contains the API context alongside other information. - :param kwargs: RPC Request data - - Returns: None - - """ - - self._send_event(context, - resource_data, - gen_cfg_const.EVENT_CONFIGURE_INTERFACES) - - def clear_interfaces(self, context, resource_data): - """Enqueues event for worker to process clear interfaces request. - - :param context: The agent info dictionary prepared in demuxer library - which contains the API context alongside other information. - :param kwargs: RPC Request data - - Returns: None - - """ - - self._send_event(context, - resource_data, - gen_cfg_const.EVENT_CLEAR_INTERFACES) - - def configure_routes(self, context, resource_data): - """Enqueues event for worker to process configure routes request. - - :param context: The agent info dictionary prepared in demuxer library - which contains the API context alongside other information. - :param kwargs: RPC Request data - - Returns: None - - """ - - self._send_event(context, - resource_data, - gen_cfg_const.EVENT_CONFIGURE_ROUTES) - - def clear_routes(self, context, resource_data): - """Enqueues event for worker to process clear routes request. - - :param context: The agent info dictionary prepared in demuxer library - which contains the API context alongside other information. - :param kwargs: RPC Request data - - Returns: None - - """ - - self._send_event(context, - resource_data, - gen_cfg_const.EVENT_CLEAR_ROUTES) - - def configure_healthmonitor(self, context, resource_data): - """Enqueues event for worker to process configure healthmonitor - request. - - :param context: The agent info dictionary prepared in demuxer library - which contains the API context alongside other information. - :param kwargs: RPC Request data - - Returns: None - - """ - LOG.info("Received configure health monitor api for nfds:" - "%(nfds)s", - {'nfds': resource_data['nfds']}) - resource_data['fail_count'] = 0 - self._send_event(context, - resource_data, - gen_cfg_const.EVENT_CONFIGURE_HEALTHMONITOR, - resource_data['nfds'][0]['vmid']) - - def clear_healthmonitor(self, context, resource_data): - """Enqueues event for worker to process clear healthmonitor request. - - :param context: The agent info dictionary prepared in demuxer library - which contains the API context alongside other information. - :param kwargs: RPC Request data - - Returns: None - - """ - LOG.info("Received clear health monitor api for nfds:" - "%(nfds)s", - {'nfds': resource_data['nfds']}) - event_key = resource_data['nfds'][0]['vmid'] - poll_event_id = gen_cfg_const.EVENT_CONFIGURE_HEALTHMONITOR - self.sc.stop_poll_event(event_key, poll_event_id) - - -class GenericConfigEventHandler(agent_base.AgentBaseEventHandler, - nfp_api.NfpEventHandler): - """Implements event handlers and their helper methods. - - Object of this class is registered with the event class of core service - controller. Based on the event key, handle_event method of this class is - invoked by core service controller. - """ - - def __init__(self, sc, drivers, rpcmgr): - super(GenericConfigEventHandler, self).__init__( - sc, drivers, rpcmgr) - self.sc = sc - - def _get_driver(self, service_type, service_vendor, service_feature): - """Retrieves service driver object based on service type input. - - Currently, service drivers are identified with service type. Support - for single driver per service type is provided. When multi-vendor - support is going to be provided, the driver should be selected based - on both service type and vendor name. - - :param service_type: Service type - firewall/vpn/loadbalancer - - Returns: Service driver instance - - """ - - return self.drivers[service_type + service_vendor + service_feature] - - def handle_event(self, ev): - """Processes the generated events in worker context. - - Processes the following events. - - Configure Interfaces - - Clear Interfaces - - Configure routes - - Clear routes - - Configure health monitor - - Clear health monitor - Enqueues responses into notification queue. - - Returns: None - - """ - try: - event_data = ev.data - if ev.id == 'PROCESS_BATCH': - NFI = event_data['sa_req_list'][0][ - 'agent_info']['context']['nfi_id'] - NF = event_data['sa_req_list'][0][ - 'agent_info']['context']['nf_id'] - else: - NFI = event_data['context']['context']['nfi_id'] - NF = event_data['context']['context']['nf_id'] - except Exception: - NFI = None - NF = None - - msg = ("Handling event '%s', with NF:%s and NFI:%s" - % (ev.id, NF, NFI)) - LOG.info(msg) - - # Process batch of request data blobs - try: - # Process batch of request data blobs - if ev.id == common_const.EVENT_PROCESS_BATCH: - self.process_batch(ev) - return - # Process HM poll events - elif ev.id == gen_cfg_const.EVENT_CONFIGURE_HEALTHMONITOR: - resource_data = ev.data.get('resource_data') - periodicity = resource_data['nfds'][0]['periodicity'] - EV_CONF_HM_MAXRETRY = ( - gen_cfg_const.EVENT_CONFIGURE_HEALTHMONITOR_MAXRETRY) - if periodicity == gen_cfg_const.INITIAL: - self.sc.poll_event( - ev, - max_times=EV_CONF_HM_MAXRETRY) - - elif periodicity == gen_cfg_const.FOREVER: - self.sc.poll_event(ev) - else: - self._process_event(ev) - except Exception as err: - msg = ("Failed to process event %s, reason %s " - % (ev.data, err)) - LOG.error(msg) - return - - def send_periodic_hm_notification(self, ev, nfd, result, notification_id): - ev_copy = copy.deepcopy(ev) - ev_copy.data["context"]["notification_data"] = {} - ev_copy.data["context"]["context"]["nfp_context"]["id"] = ( - notification_id) - ev_copy.data['context']['context']['nfd_id'] = nfd.get('vmid') - notification_data = self._prepare_notification_data(ev_copy, result) - self.notify._notification(notification_data) - - def handle_periodic_hm(self, ev, result): - resource_data = ev.data['resource_data'] - nfd = ev.data["resource_data"]['nfds'][0] - periodic_polling_reason = nfd["periodic_polling_reason"] - if result == common_const.FAILED: - """If health monitoring fails continuously for MAX_FAIL_COUNT times - send fail notification to orchestrator - """ - resource_data['fail_count'] = resource_data.get('fail_count') + 1 - if (resource_data.get('fail_count') >= - gen_cfg_const.MAX_FAIL_COUNT): - # REVISIT(Shishir): Remove statefull logic from here, - # need to come up with statleless logic. - if periodic_polling_reason == ( - gen_cfg_const.DEVICE_TO_BECOME_DOWN): - notification_id = gen_cfg_const.DEVICE_NOT_REACHABLE - self.send_periodic_hm_notification(ev, nfd, result, - notification_id) - nfd["periodic_polling_reason"] = ( - gen_cfg_const.DEVICE_TO_BECOME_UP) - elif result == common_const.SUCCESS: - """set fail_count to 0 if it had failed earlier even once - """ - resource_data['fail_count'] = 0 - if periodic_polling_reason == gen_cfg_const.DEVICE_TO_BECOME_UP: - notification_id = gen_cfg_const.DEVICE_REACHABLE - self.send_periodic_hm_notification(ev, nfd, result, - notification_id) - nfd["periodic_polling_reason"] = ( - gen_cfg_const.DEVICE_TO_BECOME_DOWN) - - def _process_event(self, ev): - LOG.debug(" Handling event %s ", (ev.data)) - # Process single request data blob - resource_data = ev.data['resource_data'] - # The context inside ev.data is the agent info dictionary prepared - # in demuxer library which contains the API context alongside - # other information like service vendor, type etc.. - agent_info = ev.data['context'] - context = agent_info['context'] - service_type = agent_info['resource_type'] - service_vendor = agent_info['service_vendor'] - service_feature = agent_info.get('service_feature', '') - - try: - msg = ("Worker process with ID: %s starting " - "to handle task: %s for service type: %s. " - % (os.getpid(), ev.id, str(service_type))) - LOG.debug(msg) - - driver = self._get_driver(service_type, service_vendor, - service_feature) - - # Invoke service driver methods based on event type received - result = getattr(driver, "%s" % ev.id.lower())(context, - resource_data) - except Exception as err: - msg = ("Failed to process ev.id=%s, ev=%s reason=%s" % - (ev.id, ev.data, err)) - LOG.error(msg) - result = common_const.FAILED - - if ev.id == gen_cfg_const.EVENT_CONFIGURE_HEALTHMONITOR: - if (resource_data['nfds'][0][ - 'periodicity'] == gen_cfg_const.INITIAL and - result == common_const.SUCCESS): - notification_data = self._prepare_notification_data(ev, - result) - self.notify._notification(notification_data) - msg = ("VM Health check successful") - LOG.info(msg) - return {'poll': False} - elif resource_data['nfds'][0]['periodicity'] == ( - gen_cfg_const.FOREVER): - ev.data["context"]["resource"] = gen_cfg_const.PERIODIC_HM - self.handle_periodic_hm(ev, result) - else: - """For other events, irrespective of result send notification""" - notification_data = self._prepare_notification_data(ev, result) - self.notify._notification(notification_data) - - def prepare_notification_result(self, result): - if result in common_const.SUCCESS: - data = {'status_code': common_const.SUCCESS} - else: - data = {'status_code': common_const.FAILURE, - 'error_msg': result} - return data - - def _prepare_notification_data(self, ev, result): - """Prepare notification data as expected by config agent - - :param ev: event object - :param result: result of the handled event - - Returns: notification_data - - """ - agent_info = ev.data['context'] - context = agent_info['context'] - - # Retrieve notification and remove it from context. Context is used - # as transport from batch processing function to this last event - # processing function. To keep the context unchanged, delete the - # notification_data before invoking driver API. - notification_data = agent_info['notification_data'] - service_type = agent_info['resource_type'] - resource = agent_info['resource'] - - data = self.prepare_notification_result(result) - msg = {'info': {'service_type': service_type, - 'context': context}, - 'notification': [{'resource': resource, - 'data': data}] - } - if not notification_data: - notification_data.update(msg) - else: - data = {'resource': resource, - 'data': data} - notification_data['notification'].append(data) - return notification_data - - def event_cancelled(self, ev, reason): - """Invoked by process framework when poll ev object reaches - polling threshold ev.max_times. - Finally it Enqueues response into notification queue. - - :param ev: Event object - - Returns: None - - """ - msg = ('Cancelled poll event. Event Data: %s ' % (ev.data)) - LOG.error(msg) - result = common_const.FAILED - notification_data = self._prepare_notification_data(ev, result) - self.notify._notification(notification_data) - - @nfp_api.poll_event_desc( - event=gen_cfg_const.EVENT_CONFIGURE_HEALTHMONITOR, - spacing=gen_cfg_const.EVENT_CONFIGURE_HEALTHMONITOR_SPACING) - def handle_configure_healthmonitor(self, ev): - """Decorator method called for poll event CONFIGURE_HEALTHMONITOR - Finally it Enqueues response into notification queue. - - :param ev: Event object - - Returns: None - - """ - return self._process_event(ev) - - -def events_init(sc, drivers, rpcmgr): - """Registers events with core service controller. - - All the events will come to handle_event method of class instance - registered in 'handler' field. - - :param drivers: Driver instances registered with the service agent - :param rpcmgr: Instance to receive all the RPC messages from configurator - module. - - Returns: None - - """ - - event_id_list = [ - gen_cfg_const.EVENT_CONFIGURE_INTERFACES, - gen_cfg_const.EVENT_CLEAR_INTERFACES, - gen_cfg_const.EVENT_CONFIGURE_ROUTES, - gen_cfg_const.EVENT_CLEAR_ROUTES, - gen_cfg_const.EVENT_CONFIGURE_HEALTHMONITOR, - gen_cfg_const.EVENT_CLEAR_HEALTHMONITOR, - common_const.EVENT_PROCESS_BATCH - ] - events = [] - - for event in event_id_list: - events.append( - nfp_event.Event( - id=event, - handler=GenericConfigEventHandler(sc, drivers, rpcmgr))) - - sc.register_events(events) - - -def load_drivers(conf): - """Imports all the driver files. - - Returns: Dictionary of driver objects with a specified service type and - vendor name - - """ - - cutils = utils.ConfiguratorUtils(conf) - drivers = cutils.load_drivers() - - for service_type, driver_name in six.iteritems(drivers): - driver_obj = driver_name(conf=conf) - drivers[service_type] = driver_obj - LOG.info("Generic config agent loaded drivers drivers:" - "%(drivers)s", - {'drivers': drivers}) - return drivers - - -def register_service_agent(cm, sc, conf, rpcmgr): - """Registers generic configuration service agent with configurator module. - - :param cm: Instance of configurator module - :param sc: Instance of core service controller - :param conf: Instance of oslo configuration - :param rpcmgr: Instance containing RPC methods which are invoked by - configurator module on corresponding RPC message arrival - - """ - - service_type = gen_cfg_const.SERVICE_TYPE - cm.register_service_agent(service_type, rpcmgr) - - -def init_agent(cm, sc, conf): - """Initializes generic configuration agent. - - :param cm: Instance of configuration module - :param sc: Instance of core service controller - :param conf: Instance of oslo configuration - - """ - - try: - drivers = load_drivers(conf) - except Exception as err: - msg = ("Generic configuration agent failed to load service drivers." - "Error:%s" - % (str(err).capitalize())) - LOG.error(msg) - raise err - else: - msg = ("Generic configuration agent loaded service" - " drivers successfully.") - LOG.debug(msg) - - rpcmgr = GenericConfigRpcManager(sc, conf) - - try: - events_init(sc, drivers, rpcmgr) - except Exception as err: - msg = ("Generic configuration agent failed to initialize events. %s" - % (str(err).capitalize())) - LOG.error(msg) - raise err - else: - msg = ("Generic configuration agent initialized" - " events successfully.") - LOG.debug(msg) - - try: - register_service_agent(cm, sc, conf, rpcmgr) - except Exception as err: - msg = ("Failed to register generic configuration agent with" - " configurator module. %s" % (str(err).capitalize())) - LOG.error(msg) - raise err - else: - msg = ("Generic configuration agent registered with configuration" - " module successfully.") - LOG.debug(msg) - - -def init_agent_complete(cm, sc, conf): - msg = ("Initialization of generic configuration agent completed.") - LOG.info(msg) diff --git a/gbpservice/contrib/nfp/configurator/agents/loadbalancer_v2.py b/gbpservice/contrib/nfp/configurator/agents/loadbalancer_v2.py deleted file mode 100644 index 72ac74cc4..000000000 --- a/gbpservice/contrib/nfp/configurator/agents/loadbalancer_v2.py +++ /dev/null @@ -1,1017 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import os - -import six - -from gbpservice.contrib.nfp.configurator.agents import agent_base -from gbpservice.contrib.nfp.configurator.lib import data_filter -from gbpservice.contrib.nfp.configurator.lib import lbv2_constants as lb_const -from gbpservice.contrib.nfp.configurator.lib import utils -from gbpservice.nfp.common import exceptions -from gbpservice.nfp.core import event as nfp_event -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.core import module as nfp_api - -LOG = nfp_logging.getLogger(__name__) - -""" Implements LBaaS response path to Neutron plugin. -Methods of this class are invoked by the LBaaSV2EventHandler class and also -by driver class for sending response from driver to the LBaaS Neutron plugin. -""" - - -class LBaaSV2RpcSender(data_filter.Filter): - - def __init__(self, sc): - self.notify = agent_base.AgentBaseNotification(sc) - - def update_status(self, obj_type, obj_id, root_lb_id, - provisioning_status, operating_status, - agent_info, obj=None): - """ Enqueues the response from LBaaS V2 operation to neutron plugin. - - :param obj_type: object type - :param obj_id: object id - :param root_lb_id: root loadbalancer id - :param provisioning_status: an enum of ('ACTIVE', 'PENDING_CREATE', - 'PENDING_UPDATE', 'PENDING_DELETE', 'ERROR') - :param operating_status: an enum of - ('ONLINE', 'OFFLINE', 'DEGRADED', 'ERROR') - - """ - - msg = {'info': {'service_type': lb_const.SERVICE_TYPE, - 'context': agent_info['context']}, - 'notification': [{'resource': agent_info['resource'], - 'data':{'obj_type': obj_type, - 'obj_id': obj_id, - 'notification_type': 'update_status', - 'root_lb_id': root_lb_id, - 'provisioning_status': - provisioning_status, - 'operating_status': - operating_status, - obj_type: obj}}] - } - LOG.info("Sending Notification 'Update Status' " - "for resource: %(resource)s with Provisioning status:" - "%(p_status)s and Operating status:%(o_status)s", - {'resource': agent_info['resource'], - 'p_status': provisioning_status, - 'o_status': operating_status}) - self.notify._notification(msg) - - # REVISIT(jiahao): need to revisit how lbaasv2 update lb stats, - # will add in visibility patch - def update_pool_stats(self, pool_id, stats, context, pool=None): - """ Enqueues the response from LBaaS operation to neutron plugin. - - :param pool_id: pool id - :param stats: statistics of that pool - - """ - msg = {'info': {'service_type': lb_const.SERVICE_TYPE, - 'context': context.to_dict()}, - 'notification': [{'resource': 'pool', - 'data': {'pool_id': pool_id, - 'stats': stats, - 'notification_type': ( - 'update_pool_stats'), - 'pool': pool_id}}] - } - LOG.info("Sending Notification 'Update Pool Stats' " - "for pool: %(pool_id)s with stats:%(stats)s", - {'pool_id': pool_id, - 'stats': stats}) - self.notify._notification(msg) - - -"""Implements APIs invoked by configurator for processing RPC messages. - -RPC client of configurator module receives RPC messages from REST server -and invokes the API of this class. The instance of this class is registered -with configurator module using register_service_agent API. Configurator module -identifies the service agent object based on service type and invokes ones of -the methods of this class to configure the device. - -""" - - -class LBaaSv2RpcManager(agent_base.AgentBaseRPCManager): - - def __init__(self, sc, conf): - """Instantiates child and parent class objects. - - :param sc: Service Controller object that is used for interfacing - with core service controller. - :param conf: Configuration object that is used for configuration - parameter access. - - """ - - super(LBaaSv2RpcManager, self).__init__(sc, conf) - - def _send_event(self, event_id, data, serialize=False, binding_key=None, - key=None): - """Posts an event to framework. - - :param event_id: Unique identifier for the event - :param event_key: Event key for serialization - :param serialize: Serialize the event - :param binding_key: binding key to be used for serialization - :param key: event key - - """ - - ev = self.sc.new_event(id=event_id, data=data) - ev.key = key - ev.sequence = serialize - ev.binding_key = binding_key - self.sc.post_event(ev) - - def create_loadbalancer(self, context, loadbalancer, driver_name): - """Enqueues event for worker to process create loadbalancer request. - - :param context: RPC context - :param loadbalancer: loadbalancer resource to be created - - Returns: None - - """ - LOG.info("Received request 'Create Loadbalancer' for LB:%(lb)s " - "with driver:%(driver_name)s", - {'lb': loadbalancer['id'], - 'driver_name': driver_name}) - arg_dict = {'context': context, - lb_const.LOADBALANCER: loadbalancer, - 'driver_name': driver_name - } - self._send_event(lb_const.EVENT_CREATE_LOADBALANCER_V2, arg_dict, - serialize=True, binding_key=loadbalancer['id'], - key=loadbalancer['id']) - - def update_loadbalancer(self, context, old_loadbalancer, loadbalancer): - """Enqueues event for worker to process update loadbalancer request. - - :param context: RPC context - :param old_loadbalancer: old loadbalancer resource to be updated - :param loadbalancer: new loadbalancer resource - - Returns: None - - """ - old_val, new_val = self.get_diff_of_dict( - old_loadbalancer, loadbalancer) - arg_dict = {'context': context, - lb_const.OLD_LOADBALANCER: old_loadbalancer, - lb_const.LOADBALANCER: loadbalancer, - } - LOG.info("Received request 'Update Loadbalancer' for LB:%(lb)s " - "with new Param:%(new_val)s and old Param:%(old_val)s", - {'lb': loadbalancer['id'], - 'new_val': new_val, - 'old_val': old_val}) - self._send_event(lb_const.EVENT_UPDATE_LOADBALANCER_V2, arg_dict, - serialize=True, binding_key=loadbalancer['id'], - key=loadbalancer['id']) - - def delete_loadbalancer(self, context, loadbalancer): - """Enqueues event for worker to process delete loadbalancer request. - - :param context: RPC context - :param loadbalancer: loadbalancer resource to be deleted - - Returns: None - - """ - LOG.info("Received request 'Delete Loadbalancer' for LB:%(lb)s ", - {'lb': loadbalancer['id']}) - - arg_dict = {'context': context, - lb_const.LOADBALANCER: loadbalancer, - } - self._send_event(lb_const.EVENT_DELETE_LOADBALANCER_V2, arg_dict, - serialize=True, binding_key=loadbalancer['id'], - key=loadbalancer['id']) - - def create_listener(self, context, listener): - """Enqueues event for worker to process create listener request. - - :param context: RPC context - :param listener: listener resource to be created - - Returns: None - - """ - LOG.info("Received request 'Create Listener' for LB:%(lb)s ", - {'lb': listener['loadbalancer_id']}) - arg_dict = {'context': context, - lb_const.LISTENER: listener, - } - self._send_event(lb_const.EVENT_CREATE_LISTENER_V2, arg_dict, - serialize=True, - binding_key=listener['loadbalancer_id'], - key=listener['id']) - - def update_listener(self, context, old_listener, listener): - """Enqueues event for worker to process update listener request. - - :param context: RPC context - :param old_listener: old listener resource to be updated - :param listener: new listener resource - - Returns: None - - """ - old_val, new_val = self.get_diff_of_dict(old_listener, listener) - LOG.info("Received request 'Update Listener' for Listener:" - "%(listener)s in LB:%(lb_id)s with new Param:" - "%(new_val)s and old Param:%(old_val)s", - {'lb_id': listener['loadbalancer_id'], - 'listener': listener['id'], - 'old_val': old_val, - 'new_val': new_val}) - arg_dict = {'context': context, - lb_const.OLD_LISTENER: old_listener, - lb_const.LISTENER: listener, - } - self._send_event(lb_const.EVENT_UPDATE_LISTENER_V2, arg_dict, - serialize=True, - binding_key=listener['loadbalancer_id'], - key=listener['id']) - - def delete_listener(self, context, listener): - """Enqueues event for worker to process delete listener request. - - :param context: RPC context - :param listener: listener resource to be deleted - - Returns: None - - """ - LOG.info("Received request 'Delete Listener' for LB:%(lb)s ", - {'lb': listener['loadbalancer_id']}) - arg_dict = {'context': context, - lb_const.LISTENER: listener, - } - self._send_event(lb_const.EVENT_DELETE_LISTENER_V2, arg_dict, - serialize=True, - binding_key=listener['loadbalancer_id'], - key=listener['id']) - - def create_pool(self, context, pool): - """Enqueues event for worker to process create pool request. - - :param context: RPC context - :param pool: pool resource to be created - - Returns: None - - """ - LOG.info("Received request 'Create Pool' for Pool:%(pool_id)s ", - {'pool_id': pool['id']}) - arg_dict = {'context': context, - lb_const.POOL: pool - } - # REVISIT(jiahao) M:N pool is not yet implemented. - self._send_event(lb_const.EVENT_CREATE_POOL_V2, arg_dict, - serialize=True, - binding_key=pool['loadbalancer_id'], - key=pool['id']) - - def update_pool(self, context, old_pool, pool): - """Enqueues event for worker to process update pool request. - - :param context: RPC context - :param old_pool: old pool resource to be updated - :param pool: new pool resource - - Returns: None - - """ - old_val, new_val = self.get_diff_of_dict(old_pool, pool) - LOG.info("Received request 'Update Pool' for Pool:%(pool)s " - "in LB:%(lb_id)s with new Param:%(new_val)s and " - "old Param:%(old_val)s", - {'pool': pool['id'], - 'lb_id': pool['loadbalancer_id'], - 'old_val': old_val, - 'new_val': new_val}) - arg_dict = {'context': context, - lb_const.OLD_POOL: old_pool, - lb_const.POOL: pool, - } - self._send_event(lb_const.EVENT_UPDATE_POOL_V2, arg_dict, - serialize=True, - binding_key=pool['loadbalancer_id'], - key=pool['id']) - - def delete_pool(self, context, pool): - """Enqueues event for worker to process delete pool request. - - :param context: RPC context - :param pool: pool resource to be deleted - - Returns: None - - """ - LOG.info("Received request 'Delete Pool' for Pool:%(pool_id)s ", - {'pool_id': pool['id']}) - arg_dict = {'context': context, - lb_const.POOL: pool, - } - self._send_event(lb_const.EVENT_DELETE_POOL_V2, arg_dict, - serialize=True, - binding_key=pool['loadbalancer_id'], - key=pool['id']) - - def create_member(self, context, member): - """Enqueues event for worker to process create member request. - - :param context: RPC context - :param member: member resource to be created - - Returns: None - - """ - LOG.info("Received request 'Create Member' for Pool:%(pool_id)s ", - {'pool_id': member['pool_id']}) - arg_dict = {'context': context, - lb_const.MEMBER: member, - } - self._send_event(lb_const.EVENT_CREATE_MEMBER_V2, arg_dict, - serialize=True, - binding_key=member[lb_const.POOL]['loadbalancer_id'], - key=member['id']) - - def update_member(self, context, old_member, member): - """Enqueues event for worker to process update member request. - - :param context: RPC context - :param old_member: old member resource to be updated - :param member: new member resource - - Returns: None - - """ - old_val, new_val = self.get_diff_of_dict(old_member, member) - LOG.info("Received request 'Update Member' for Member:" - "%(member_id)s in Pool:%(pool_id)s with new Param:" - "%(new_val)s and old Param:%(old_val)s", - {'pool_id': member['pool_id'], - 'member_id': member['id'], - 'old_val': old_val, - 'new_val': new_val}) - arg_dict = {'context': context, - lb_const.OLD_MEMBER: old_member, - lb_const.MEMBER: member, - } - self._send_event(lb_const.EVENT_UPDATE_MEMBER_V2, arg_dict, - serialize=True, - binding_key=member[lb_const.POOL]['loadbalancer_id'], - key=member['id']) - - def delete_member(self, context, member): - """Enqueues event for worker to process delete member request. - - :param context: RPC context - :param member: member resource to be deleted - - Returns: None - - """ - LOG.info("Received request 'Delete Member' for Pool:" - "%(pool_id)s ", - {'pool_id': member['pool_id']}) - arg_dict = {'context': context, - lb_const.MEMBER: member, - } - self._send_event(lb_const.EVENT_DELETE_MEMBER_V2, arg_dict, - serialize=True, - binding_key=member[lb_const.POOL]['loadbalancer_id'], - key=member['id']) - - def create_healthmonitor(self, context, healthmonitor): - """Enqueues event for worker to process create health monitor request. - - :param context: RPC context - :param health_monitor: health_monitor resource to be created - :param pool_id: pool_id to which health monitor is associated - - Returns: None - - """ - LOG.info("Received request 'Create Pool Health Monitor' for" - "Health monitor:%(hm)s", - {'hm': healthmonitor['id']}) - arg_dict = {'context': context, - lb_const.HEALTHMONITOR: healthmonitor - } - self._send_event(lb_const.EVENT_CREATE_HEALTH_MONITOR_V2, - arg_dict, serialize=True, - binding_key=healthmonitor[lb_const.POOL][ - 'loadbalancer_id'], - key=healthmonitor['id']) - - def update_healthmonitor(self, context, old_healthmonitor, healthmonitor): - """Enqueues event for worker to process update health monitor request. - - :param context: RPC context - :param old_health_monitor: health_monitor resource to be updated - :param health_monitor: new health_monitor resource - :param pool_id: pool_id to which health monitor is associated - - Returns: None - - """ - old_val, new_val = self.get_diff_of_dict( - old_healthmonitor, healthmonitor) - LOG.info("Received request 'Update Pool Health Monitor' for " - "Health monitor:%(hm)s with new Param:%(new_val)s and " - "old Param:%(old_val)s", - {'hm': healthmonitor['id'], - 'old_val': old_val, - 'new_val': new_val}) - arg_dict = {'context': context, - lb_const.OLD_HEALTHMONITOR: old_healthmonitor, - lb_const.HEALTHMONITOR: healthmonitor - } - self._send_event(lb_const.EVENT_UPDATE_HEALTH_MONITOR_V2, - arg_dict, serialize=True, - binding_key=healthmonitor[lb_const.POOL][ - 'loadbalancer_id'], - key=healthmonitor['id']) - - def delete_healthmonitor(self, context, healthmonitor): - """Enqueues event for worker to process delete health monitor request. - - :param context: RPC context - :param health_monitor: health_monitor resource to be deleted - :param pool_id: pool_id to which health monitor is associated - - Returns: None - - """ - LOG.info("Received request 'Delete Pool Health Monitor' for " - "Health monitor:%(hm)s", - {'hm': healthmonitor['id']}) - arg_dict = {'context': context, - lb_const.HEALTHMONITOR: healthmonitor - } - self._send_event(lb_const.EVENT_DELETE_HEALTH_MONITOR_V2, - arg_dict, serialize=True, - binding_key=healthmonitor[lb_const.POOL][ - 'loadbalancer_id'], - key=healthmonitor['id']) - - def agent_updated(self, context, payload): - """Enqueues event for worker to process agent updated request. - - :param context: RPC context - :param payload: payload - - Returns: None - - """ - LOG.info("Received request 'Agent Updated' ") - arg_dict = {'context': context, - 'payload': payload} - self._send_event(lb_const.EVENT_AGENT_UPDATED_V2, arg_dict) - - -"""Implements event handlers and their helper methods. - -Object of this class is registered with the event class of core service -controller. Based on the event key, handle_event method of this class is -invoked by core service controller. - -""" - - -class LBaaSV2EventHandler(agent_base.AgentBaseEventHandler, - nfp_api.NfpEventHandler): - instance_mapping = {} - - def __init__(self, sc, drivers, rpcmgr): - self.sc = sc - self.drivers = drivers - self.rpcmgr = rpcmgr - self.plugin_rpc = LBaaSV2RpcSender(sc) - - def _get_driver(self, driver_name): - """Retrieves service driver object based on service type input. - - Currently, service drivers are identified with service type. Support - for single driver per service type is provided. When multi-vendor - support is going to be provided, the driver should be selected based - on both service type and vendor name. - - :param service_type: Service type - loadbalancer - - Returns: Service driver instance - - """ - driver = lb_const.SERVICE_TYPE + driver_name - return self.drivers[driver] - - def _root_loadbalancer_id(self, obj_type, obj_dict): - """Returns the loadbalancer id this instance is attached to.""" - - try: - # For Mitaka - if obj_type == lb_const.LOADBALANCER: - lb = obj_dict['id'] - elif obj_type == lb_const.LISTENER: - lb = obj_dict[lb_const.LOADBALANCER]['id'] - elif obj_type == lb_const.L7POLICY: - lb = obj_dict[lb_const.LISTENER][lb_const.LOADBALANCER]['id'] - elif obj_type == lb_const.L7RULE: - lb = obj_dict['policy'][lb_const.LISTENER][ - lb_const.LOADBALANCER]['id'] - elif obj_type == lb_const.POOL: - lb = obj_dict[lb_const.LOADBALANCER]['id'] - elif obj_type == lb_const.SNI: - lb = obj_dict[lb_const.LISTENER][lb_const.LOADBALANCER]['id'] - else: - # Pool Member or Health Monitor - lb = obj_dict[lb_const.POOL][lb_const.LOADBALANCER]['id'] - # For Liberty - # if obj_type == lb_const.LOADBALANCER: - # lb = obj_dict['id'] - # elif obj_type == lb_const.LISTENER: - # lb = obj_dict[lb_const.LOADBALANCER]['id'] - # elif obj_type == lb_const.POOL: - # lb = obj_dict[lb_const.LISTENER][lb_const.LOADBALANCER]['id'] - # elif obj_type == lb_const.SNI: - # lb = obj_dict[lb_const.LISTENER][lb_const.LOADBALANCER]['id'] - # else: - # # Pool Member or Health Monitor - # lb = obj_dict[lb_const.POOL][lb_const.LISTENER][ - # lb_const.LOADBALANCER]['id'] - except Exception: - raise exceptions.IncompleteData( - 'Root loadbalancer id was not found') - else: - return lb - - def handle_event(self, ev): - """Processes the generated events in worker context. - - Processes the following events. - - create loadbalancer - - update loadbalancer - - delete loadbalancer - - create listener - - update listener - - delete listener - - create pool - - update pool - - delete pool - - create member - - update member - - delete member - - create health monitor - - update health monitor - - delete health monitor - - agent updated - Enqueues responses into notification queue. - - Returns: None - - """ - msg = ("Handling event '%s' " % (ev.id)) - LOG.info(msg) - try: - msg = ("Worker process with ID: %s starting " - "to handle task: %s of topic: %s. " - % (os.getpid(), ev.id, lb_const.LBAAS_AGENT_RPC_TOPIC)) - LOG.debug(msg) - - method = getattr(self, "_%s" % (ev.id.lower())) - method(ev) - except Exception as err: - msg = ("Failed to perform the operation: %s. %s" - % (ev.id, str(err).capitalize())) - LOG.error(msg) - finally: - if ev.id == lb_const.EVENT_COLLECT_STATS_V2: - """Do not say event done for collect stats as it is - to be executed forever - """ - pass - else: - msg = ("Calling event done for event '%s' " % (ev.id)) - LOG.info(msg) - self.sc.event_complete(ev) - - def _handle_event_loadbalancer(self, ev, operation): - data = ev.data - context = data['context'] - loadbalancer = data[lb_const.LOADBALANCER] - root_lb_id = self._root_loadbalancer_id( - lb_const.LOADBALANCER, loadbalancer) - agent_info = ev.data['context'].get('agent_info') - service_vendor = agent_info['service_vendor'] - - try: - if operation == lb_const.CREATE: - driver_name = data['driver_name'] - driver_id = driver_name + service_vendor - if (driver_id) not in list(self.drivers.keys()): - msg = ('No device driver on agent: %s.' % (driver_name)) - LOG.error(msg) - self.plugin_rpc.update_status( - lb_const.LOADBALANCER, loadbalancer['id'], root_lb_id, - lb_const.ERROR, lb_const.OFFLINE, agent_info, - None) - return - driver = self.drivers[driver_id] - driver.load_balancer.create(context, loadbalancer) - LBaaSV2EventHandler.instance_mapping[loadbalancer['id']] \ - = driver_name - elif operation == lb_const.UPDATE: - old_loadbalancer = data[lb_const.OLD_LOADBALANCER] - driver = self._get_driver(service_vendor) - driver.load_balancer.update(context, - old_loadbalancer, loadbalancer) - elif operation == lb_const.DELETE: - driver = self._get_driver(service_vendor) - driver.load_balancer.delete(context, loadbalancer) - del LBaaSV2EventHandler.instance_mapping[loadbalancer['id']] - return # Don't update object status for delete operation - except Exception: - if operation == lb_const.DELETE: - msg = ( - "Failed to delete loadbalancer %s" % (loadbalancer['id'])) - LOG.warning(msg) - del LBaaSV2EventHandler.instance_mapping[loadbalancer['id']] - else: - self.plugin_rpc.update_status( - lb_const.LOADBALANCER, loadbalancer['id'], root_lb_id, - lb_const.ERROR, lb_const.OFFLINE, - agent_info, None) - else: - self.plugin_rpc.update_status( - lb_const.LOADBALANCER, loadbalancer['id'], root_lb_id, - lb_const.ACTIVE, lb_const.ONLINE, - agent_info, None) - - def _create_loadbalancer_v2(self, ev): - self._handle_event_loadbalancer(ev, lb_const.CREATE) - - def _update_loadbalancer_v2(self, ev): - self._handle_event_loadbalancer(ev, lb_const.UPDATE) - - def _delete_loadbalancer_v2(self, ev): - self._handle_event_loadbalancer(ev, lb_const.DELETE) - - def _handle_event_listener(self, ev, operation): - data = ev.data - context = data['context'] - listener = data[lb_const.LISTENER] - root_lb_id = self._root_loadbalancer_id(lb_const.LISTENER, listener) - agent_info = ev.data['context'].get('agent_info') - service_vendor = agent_info['service_vendor'] - driver = self._get_driver(service_vendor) - - try: - if operation == lb_const.CREATE: - driver.listener.create(context, listener) - elif operation == lb_const.UPDATE: - old_listener = data[lb_const.OLD_LISTENER] - driver.listener.update(context, old_listener, listener) - elif operation == lb_const.DELETE: - driver.listener.delete(context, listener) - return # Don't update object status for delete operation - except Exception: - if operation == lb_const.DELETE: - msg = ("Failed to delete listener %s" % (listener['id'])) - LOG.warning(msg) - else: - self.plugin_rpc.update_status( - lb_const.LISTENER, listener['id'], root_lb_id, - lb_const.ERROR, lb_const.OFFLINE, - agent_info, None) - else: - self.plugin_rpc.update_status( - lb_const.LISTENER, listener['id'], root_lb_id, - lb_const.ACTIVE, lb_const.ONLINE, - agent_info, None) - - def _create_listener_v2(self, ev): - self._handle_event_listener(ev, lb_const.CREATE) - - def _update_listener_v2(self, ev): - self._handle_event_listener(ev, lb_const.UPDATE) - - def _delete_listener_v2(self, ev): - self._handle_event_listener(ev, lb_const.DELETE) - - def _handle_event_pool(self, ev, operation): - data = ev.data - context = data['context'] - pool = data[lb_const.POOL] - root_lb_id = self._root_loadbalancer_id(lb_const.POOL, pool) - agent_info = ev.data['context'].get('agent_info') - service_vendor = agent_info['service_vendor'] - driver = self._get_driver(service_vendor) - - try: - if operation == lb_const.CREATE: - driver.pool.create(context, pool) - elif operation == lb_const.UPDATE: - old_pool = data[lb_const.OLD_POOL] - driver.pool.update(context, old_pool, pool) - elif operation == lb_const.DELETE: - driver.pool.delete(context, pool) - return # Don't update object status for delete operation - except Exception: - if operation == lb_const.DELETE: - msg = "Failed to delete pool %s" % (pool['id']) - LOG.warning(msg) - else: - self.plugin_rpc.update_status( - lb_const.POOL, pool['id'], root_lb_id, - lb_const.ERROR, lb_const.OFFLINE, - agent_info, None) - else: - self.plugin_rpc.update_status( - lb_const.POOL, pool['id'], root_lb_id, - lb_const.ACTIVE, lb_const.ONLINE, - agent_info, None) - - def _create_pool_v2(self, ev): - self._handle_event_pool(ev, lb_const.CREATE) - - def _update_pool_v2(self, ev): - self._handle_event_pool(ev, lb_const.UPDATE) - - def _delete_pool_v2(self, ev): - self._handle_event_pool(ev, lb_const.DELETE) - - def _handle_event_member(self, ev, operation): - data = ev.data - context = data['context'] - member = data[lb_const.MEMBER] - root_lb_id = self._root_loadbalancer_id(lb_const.MEMBER, member) - agent_info = ev.data['context'].get('agent_info') - service_vendor = agent_info['service_vendor'] - driver = self._get_driver(service_vendor) # member['pool_id']) - try: - if operation == lb_const.CREATE: - driver.member.create(context, member) - elif operation == lb_const.UPDATE: - old_member = data[lb_const.OLD_MEMBER] - driver.member.update(context, old_member, member) - elif operation == lb_const.DELETE: - driver.member.delete(context, member) - return # Don't update object status for delete operation - except Exception: - if operation == lb_const.DELETE: - msg = ("Failed to delete member %s" % (member['id'])) - LOG.warning(msg) - else: - self.plugin_rpc.update_status( - lb_const.MEMBER, member['id'], root_lb_id, - lb_const.ERROR, lb_const.OFFLINE, - agent_info, None) - else: - self.plugin_rpc.update_status( - lb_const.MEMBER, member['id'], root_lb_id, - lb_const.ACTIVE, lb_const.ONLINE, - agent_info, None) - - def _create_member_v2(self, ev): - self._handle_event_member(ev, lb_const.CREATE) - - def _update_member_v2(self, ev): - self._handle_event_member(ev, lb_const.UPDATE) - - def _delete_member_v2(self, ev): - self._handle_event_member(ev, lb_const.DELETE) - - def _handle_event_health_monitor(self, ev, operation): - data = ev.data - context = data['context'] - healthmonitor = data[lb_const.HEALTHMONITOR] - root_lb_id = self._root_loadbalancer_id( - lb_const.HEALTHMONITOR, healthmonitor) - agent_info = context.get('agent_info') - service_vendor = agent_info['service_vendor'] - driver = self._get_driver(service_vendor) # (pool_id) - - pool_id = healthmonitor[lb_const.POOL]['id'] - assoc_id = {'pool_id': pool_id, - 'monitor_id': healthmonitor['id']} - try: - if operation == lb_const.CREATE: - driver.health_monitor.create(context, healthmonitor) - elif operation == lb_const.UPDATE: - old_healthmonitor = data[lb_const.OLD_HEALTHMONITOR] - driver.health_monitor.update(context, old_healthmonitor, - healthmonitor) - elif operation == lb_const.DELETE: - driver.health_monitor.delete(context, healthmonitor) - return # Don't update object status for delete operation - except Exception: - if operation == lb_const.DELETE: - msg = ("Failed to delete pool health monitor." - " assoc_id: %s" % (assoc_id)) - LOG.warning(msg) - else: - self.plugin_rpc.update_status( - lb_const.HEALTHMONITOR, healthmonitor['id'], root_lb_id, - lb_const.ERROR, lb_const.OFFLINE, - agent_info, None) - else: - self.plugin_rpc.update_status( - lb_const.HEALTHMONITOR, healthmonitor['id'], root_lb_id, - lb_const.ACTIVE, lb_const.ONLINE, - agent_info, None) - - def _create_health_monitor_v2(self, ev): - self._handle_event_health_monitor(ev, lb_const.CREATE) - - def _update_health_monitor_v2(self, ev): - self._handle_event_health_monitor(ev, lb_const.UPDATE) - - def _delete_health_monitor_v2(self, ev): - self._handle_event_health_monitor(ev, lb_const.DELETE) - - def _agent_updated(self, ev): - """ REVISIT(pritam): Support """ - return None - - def _collect_stats(self, ev): - self.sc.poll_event(ev) - - @nfp_api.poll_event_desc(event=lb_const.EVENT_COLLECT_STATS_V2, - spacing=60) - def collect_stats_v2(self, ev): - for pool_id, driver_name in \ - list(LBaaSV2EventHandler.instance_mapping.items()): - driver_id = lb_const.SERVICE_TYPE + driver_name - driver = self.drivers[driver_id] - try: - stats = driver.get_stats(pool_id) - if stats: - self.plugin_rpc.update_pool_stats(pool_id, stats, - self.context) - except Exception: - msg = ("Error updating statistics on pool %s" % (pool_id)) - LOG.error(msg) - - -def events_init(sc, drivers, rpcmgr): - """Registers events with core service controller. - - All the events will come to handle_event method of class instance - registered in 'handler' field. - - :param drivers: Driver instances registered with the service agent - :param rpcmgr: Instance to receive all the RPC messages from configurator - module. - - Returns: None - - """ - ev_ids = [lb_const.EVENT_CREATE_LOADBALANCER_V2, - lb_const.EVENT_UPDATE_LOADBALANCER_V2, - lb_const.EVENT_DELETE_LOADBALANCER_V2, - - lb_const.EVENT_CREATE_LISTENER_V2, - lb_const.EVENT_UPDATE_LISTENER_V2, - lb_const.EVENT_DELETE_LISTENER_V2, - - lb_const.EVENT_CREATE_POOL_V2, lb_const.EVENT_UPDATE_POOL_V2, - lb_const.EVENT_DELETE_POOL_V2, - - lb_const.EVENT_CREATE_MEMBER_V2, - lb_const.EVENT_UPDATE_MEMBER_V2, - lb_const.EVENT_DELETE_MEMBER_V2, - - lb_const.EVENT_CREATE_HEALTH_MONITOR_V2, - lb_const.EVENT_UPDATE_HEALTH_MONITOR_V2, - lb_const.EVENT_DELETE_HEALTH_MONITOR_V2, - - lb_const.EVENT_AGENT_UPDATED_V2, - lb_const.EVENT_COLLECT_STATS_V2 - ] - - evs = [] - for ev_id in ev_ids: - ev = nfp_event.Event(id=ev_id, handler=LBaaSV2EventHandler( - sc, drivers, rpcmgr)) - evs.append(ev) - sc.register_events(evs) - - -def load_drivers(sc, conf): - """Imports all the driver files. - - Returns: Dictionary of driver objects with a specified service type and/or - vendor name - - """ - cutils = utils.ConfiguratorUtils(conf) - drivers = cutils.load_drivers(lb_const.SERVICE_TYPE) - - plugin_rpc = LBaaSV2RpcSender(sc) - - for service_type, dobj in six.iteritems(drivers): - '''LB Driver constructor needs plugin_rpc as a param''' - instantiated_dobj = dobj(plugin_rpc=plugin_rpc, conf=conf) - drivers[service_type] = instantiated_dobj - - return drivers - - -def register_service_agent(cm, sc, conf, rpcmgr): - """Registers Loadbalaner V2 service agent with configurator module. - - :param cm: Instance of configurator module - :param sc: Instance of core service controller - :param conf: Instance of oslo configuration - :param rpcmgr: Instance containing RPC methods which are invoked by - configurator module on corresponding RPC message arrival - - """ - - service_type = lb_const.SERVICE_TYPE - cm.register_service_agent(service_type, rpcmgr) - - -def init_agent(cm, sc, conf): - """Initializes Loadbalaner V2 agent. - - :param cm: Instance of configuration module - :param sc: Instance of core service controller - :param conf: Instance of oslo configuration - - """ - - try: - drivers = load_drivers(sc, conf) - except Exception as err: - msg = ("Loadbalaner V2 agent failed to load service drivers. %s" - % (str(err).capitalize())) - LOG.error(msg) - raise err - else: - msg = ("Loadbalaner V2 agent loaded service" - " drivers successfully.") - LOG.debug(msg) - - rpcmgr = LBaaSv2RpcManager(sc, conf) - - try: - events_init(sc, drivers, rpcmgr) - except Exception as err: - msg = ("Loadbalaner V2 agent failed to initialize events. %s" - % (str(err).capitalize())) - LOG.error(msg) - raise err - else: - msg = ("Loadbalaner V2 agent initialized" - " events successfully.") - LOG.debug(msg) - - try: - register_service_agent(cm, sc, conf, rpcmgr) - except Exception as err: - msg = ("Failed to register Loadbalaner V2 agent with" - " configurator module. %s" % (str(err).capitalize())) - LOG.error(msg) - raise err - else: - msg = ("Loadbalaner V2 agent registered with configuration" - " module successfully.") - LOG.debug(msg) - - -def _start_collect_stats(sc): - """Enqueues poll event for worker to collect pool stats periodically. - Agent keeps map of pool_id:driver. As part of handling this event, - stats for pool_id is requested from agent inside service vm - """ - - arg_dict = {} - ev = sc.new_event(id=lb_const.EVENT_COLLECT_STATS_V2, data=arg_dict) - sc.post_event(ev) - - -def init_agent_complete(cm, sc, conf): - # _start_collect_stats(sc) - msg = ("Initialization of loadbalancer agent v2 completed.") - LOG.info(msg) diff --git a/gbpservice/contrib/nfp/configurator/agents/nfp_service.py b/gbpservice/contrib/nfp/configurator/agents/nfp_service.py deleted file mode 100644 index b9616f3b0..000000000 --- a/gbpservice/contrib/nfp/configurator/agents/nfp_service.py +++ /dev/null @@ -1,255 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import os - -import oslo_messaging as messaging -import six - -from gbpservice.contrib.nfp.configurator.agents import agent_base -from gbpservice.contrib.nfp.configurator.lib import ( - nfp_service_constants as const) -from gbpservice.contrib.nfp.configurator.lib import utils as load_driver -from gbpservice.nfp.core import event as nfp_event -from gbpservice.nfp.core import log as nfp_logging - -LOG = nfp_logging.getLogger(__name__) - - -class ConfigScriptRpcManager(agent_base.AgentBaseRPCManager): - """ Implements ConfigScriptRpcManager class which receives requests - from Configurator module. - - Methods of this class are invoked by the configurator. Events are - created according to the requests received and enqueued to worker queues. - - """ - - RPC_API_VERSION = '1.0' - target = messaging.Target(version=RPC_API_VERSION) - - def __init__(self, sc, conf): - """Instantiates child and parent class objects. - - :param sc: Service Controller object that is used to communicate - with process model core file. - :param conf: Configuration object that is used for configuration - parameter access. - - """ - - super(ConfigScriptRpcManager, self).__init__(sc, conf) - - def run_nfp_service(self, context, resource_data): - """ Receives request to execute config script. - - :param context: RPC context - :param kwargs: Contains configuration script and request information - - """ - - msg = ("ConfigScriptRpcManager received Create Heat request.") - LOG.debug(msg) - - arg_dict = {'context': context, - 'resource_data': resource_data} - ev = self.sc.new_event(id=const.CREATE_NFP_SERVICE_EVENT, - data=arg_dict, key=None) - self.sc.post_event(ev) - - -class ConfigScriptEventHandler(agent_base.AgentBaseEventHandler): - """ Handler class which invokes nfp_service driver methods - - Worker processes dequeue the worker queues and invokes the - appropriate handler class methods for ConfigScript methods. - - """ - - def __init__(self, sc, drivers, rpcmgr): - """ Initializes parent and child class objects. - - :param sc: Service Controller object that is used to communicate - with process model. - :param drivers: Dictionary of driver name to object mapping - :param rpcmgr: ConfigScriptRpcManager class object - - """ - - super(ConfigScriptEventHandler, self).__init__(sc, drivers, rpcmgr) - self.sc = sc - self.drivers = drivers - self.rpcmgr = rpcmgr - - def _get_driver(self): - """ Retrieves driver object given the service type. - - """ - - driver_id = const.SERVICE_TYPE - return self.drivers[driver_id] - - def handle_event(self, ev): - """ Demultiplexes the nfp_service request to appropriate - driver methods. - - :param ev: Event object sent from process model event handler - - """ - - try: - agent_info = ev.data['context'] - notification_context = agent_info['context'] - resource = agent_info['resource'] - resource_data = ev.data['resource_data'] - - msg = ("Worker process with ID: %s starting to " - "handle task: %s of type ConfigScript. " - % (os.getpid(), ev.id)) - LOG.debug(msg) - - driver = self._get_driver() - self.method = getattr(driver, "run_%s" % resource) - - result = self.method(notification_context, resource_data) - except Exception as err: - result = const.ERROR_RESULT - msg = ("Failed to handle event: %s. %s" - % (ev.id, str(err).capitalize())) - LOG.error(msg) - finally: - del agent_info['notification_data'] - del agent_info['service_vendor'] - service_type = agent_info.pop('resource_type') - - if result in const.UNHANDLED_RESULT: - data = {'status_code': const.UNHANDLED_RESULT} - else: - data = {'status_code': const.FAILURE, - 'error_msg': result} - - msg = {'info': {'service_type': service_type, - 'context': notification_context}, - 'notification': [{'resource': resource, - 'data': data}] - } - - self.notify._notification(msg) - - -def events_init(sc, drivers, rpcmgr): - """Registers events with core service controller. - - All the events will come to handle_event method of class instance - registered in 'handler' field. - - :param drivers: Driver instances registered with the service agent - :param rpcmgr: Instance to receive all the RPC messages from configurator - module. - - Returns: None - - """ - - event = nfp_event.Event( - id=const.CREATE_NFP_SERVICE_EVENT, - handler=ConfigScriptEventHandler(sc, drivers, rpcmgr)) - sc.register_events([event]) - - -def load_drivers(conf): - """Imports all the driver files corresponding to this agent. - - Returns: Dictionary of driver objects with a specified service type and - vendor name - - """ - - ld = load_driver.ConfiguratorUtils(conf) - drivers = ld.load_drivers(const.SERVICE_TYPE) - - for service_type, driver_name in six.iteritems(drivers): - driver_obj = driver_name(conf=conf) - drivers[service_type] = driver_obj - - return drivers - - -def register_service_agent(cm, sc, conf, rpcmgr): - """Registers ConfigScript service agent with configurator module. - - :param cm: Instance of configurator module - :param sc: Instance of core service controller - :param conf: Instance of oslo configuration - :param rpcmgr: Instance containing RPC methods which are invoked by - configurator module on corresponding RPC message arrival - - """ - - service_type = const.SERVICE_TYPE - cm.register_service_agent(service_type, rpcmgr) - - -def init_agent(cm, sc, conf): - """Initializes Config Script agent. - - :param cm: Instance of configuration module - :param sc: Instance of core service controller - :param conf: Instance of oslo configuration - - """ - - try: - drivers = load_drivers(conf) - except Exception as err: - msg = ("Config Script failed to load drivers. %s" - % (str(err).capitalize())) - LOG.error(msg) - raise Exception(err) - else: - msg = ("Config Script loaded drivers successfully.") - LOG.debug(msg) - - rpcmgr = ConfigScriptRpcManager(sc, conf) - try: - events_init(sc, drivers, rpcmgr) - except Exception as err: - msg = ("Config Script Events initialization unsuccessful. %s" - % (str(err).capitalize())) - LOG.error(msg) - raise Exception(err) - else: - msg = ("Config Script Events initialization successful.") - LOG.debug(msg) - - try: - register_service_agent(cm, sc, conf, rpcmgr) - except Exception as err: - msg = ("Config Script service agent registration unsuccessful. %s" - % (str(err).capitalize())) - LOG.error(msg) - raise Exception(err) - else: - msg = ("Config Script service agent registration successful.") - LOG.debug(msg) - - msg = ("ConfigScript as a Service Module Initialized.") - LOG.info(msg) - - -def init_agent_complete(cm, sc, conf): - """ Initializes periodic tasks - - """ - - msg = (" Config Script agent init complete") - LOG.info(msg) diff --git a/gbpservice/contrib/nfp/configurator/agents/vpn.py b/gbpservice/contrib/nfp/configurator/agents/vpn.py deleted file mode 100644 index fbc324f0e..000000000 --- a/gbpservice/contrib/nfp/configurator/agents/vpn.py +++ /dev/null @@ -1,461 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - - -import os - -import oslo_messaging as messaging -import six - -from gbpservice.contrib.nfp.configurator.agents import agent_base -from gbpservice.contrib.nfp.configurator.drivers.base import base_driver -from gbpservice.contrib.nfp.configurator.lib import data_filter -from gbpservice.contrib.nfp.configurator.lib import utils -from gbpservice.contrib.nfp.configurator.lib import vpn_constants as const -from gbpservice.nfp.core import event as nfp_event -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.core import module as nfp_api - -LOG = nfp_logging.getLogger(__name__) - - -class VpnaasRpcSender(data_filter.Filter): - """ - Implements VPNaas response path to Neutron plugin. - - Methods of this class are invoked by the VPNaasEventHandler class - for sending response from driver to the VPNaas Neutron plugin. - - """ - RPC_API_VERSION = '1.0' - target = messaging.Target(version=RPC_API_VERSION) - - def __init__(self, sc): - self._sc = sc - self._notify = agent_base.AgentBaseNotification(sc) - super(VpnaasRpcSender, self).__init__(None, None) - - def get_vpn_services(self, context, ids=None, filters=None): - """Gets list of vpnservices for tenant. - :param context: dictionary which holds details of vpn service type like - For IPSEC connections : - List of vpnservices - lIst of ipsec connections - ike policy & ipsec policy. - :param ids: based on which the filter library extracts the data. - :param filter: based on which the filter library extracts the data. - - Returns: Dictionary of vpn service type which matches with the filters. - """ - LOG.info("Sending RPC for GET VPN SERVICES with %(filters)s ", - {'filters': filters}) - return self.call( - context, - self.make_msg('get_vpn_services', ids=ids, filters=filters)) - - def get_vpn_servicecontext(self, context, filters=None): - """Get list of vpnservice context on this host. - :param context: dictionary which holds details of vpn service type like - For IPSEC connections : - List of vpnservices - lIst of ipsec connections - ike policy & ipsec policy. - :param filter: based on which the filter library extracts the data - from context dictionary. - - Returns: dictionary of vpnservice - """ - LOG.info("Sending RPC for GET VPN SERVICECONTEXT with " - "Filters:%(filters)s ", - {'filters': filters}) - return self.call( - context, - self.make_msg( - 'get_vpn_servicecontext', filters=filters)) - - def get_ipsec_conns(self, context, filters): - """ - Get list of ipsec conns with filters - specified. - """ - LOG.info("Sending RPC for GET IPSEC CONNS with Filters:" - "%(filters)s ", - {'filters': filters}) - return self.call( - context, - self.make_msg( - 'get_ipsec_conns', - filters=filters)) - - def update_status(self, context, status): - """Update local status. - - This method call updates status attribute of - VPNServices. - """ - msg = {'info': {'service_type': const.SERVICE_TYPE, - 'context': context['agent_info']['context']}, - 'notification': [{ - 'resource': context['agent_info']['resource'], - 'data': {'status': status, - 'notification_type': ( - 'update_status')}}] - } - LOG.info("Sending Notification 'Update Status' with " - "status:%(status)s ", - {'status': status}) - self._notify._notification(msg) - - def ipsec_site_conn_deleted(self, context, resource_id): - """ Notify VPNaaS plugin about delete of ipsec-site-conn """ - - msg = {'info': {'service_type': const.SERVICE_TYPE, - 'context': context['agent_info']['context']}, - 'notification': [{ - 'resource': context['agent_info']['resource'], - 'data': {'resource_id': resource_id, - 'notification_type': ( - 'ipsec_site_conn_deleted')}}] - } - LOG.info("Sending Notification 'Ipsec Site Conn Deleted' " - "for resource:%(resource_id)s ", - {'resource_id': resource_id}) - self._notify._notification(msg) - - -class VPNaasRpcManager(agent_base.AgentBaseRPCManager): - """ - Implements VPNaasRpcManager class which receives requests - from Configurator to Agent. - - Methods of this class are invoked by the configurator. Events are - created according to the requests received and enqueued to worker queues. - - """ - - RPC_API_VERSION = '1.0' - target = messaging.Target(version=RPC_API_VERSION) - - def __init__(self, conf, sc): - """Instantiates child and parent class objects. - - Passes the instances of core service controller and oslo configuration - to parent instance in order to provide event enqueue facility for batch - processing event. - - :param sc: Service Controller object that is used for interfacing - with core service controller. - :param conf: Configuration object that is used for configuration - parameter access. - - """ - - super(VPNaasRpcManager, self).__init__(sc, conf) - - def vpnservice_updated(self, context, **resource_data): - """Registers the VPNaas plugin events to update the vpn configurations. - - :param context: dictionary, confined to the specific service type. - :param resource_data: dictionary, confined to the specific - operation type. - - Returns: None - """ - - LOG.info("Received request 'VPN Service Updated'." - "for API '%(api)s'", - {'api': resource_data.get('reason', '')}) - arg_dict = {'context': context, - 'resource_data': resource_data} - # Serializing the event because simultaneous configure - # requests overrides the same crypto-map in the service VM - # which results in corrupting the crypto-map - - resource_type = resource_data.get('rsrc_type') - if resource_type and resource_type.lower() == 'ipsec_site_connection': - ev = self.sc.new_event(id='VPNSERVICE_UPDATED', - key=resource_data['resource']['id'], - data=arg_dict, - serialize=True, - binding_key=resource_data[ - 'resource']['vpnservice_id']) - msg = "serializing event: %s" % ('VPNSERVICE_UPDATED') - LOG.debug(msg) - else: - ev = self.sc.new_event(id='VPNSERVICE_UPDATED', data=arg_dict) - - self.sc.post_event(ev) - - -class VPNaasEventHandler(nfp_api.NfpEventHandler): - """ - Handler class to invoke the vpn driver methods. - For every event that gets invoked from worker process lands over here - to make a call to the driver methods. - """ - - def __init__(self, sc, drivers): - """ Instantiates class object. - - :param sc: Service Controller object that is used to communicate - with process model core file. - :param drivers: dictionary of driver name to object mapping - - """ - self._sc = sc - self._drivers = drivers - self._plugin_rpc = VpnaasRpcSender(self._sc) - - def _get_driver(self, service_vendor, service_feature): - - driver_id = const.SERVICE_TYPE + service_vendor + service_feature - return self._drivers[driver_id] - - def handle_event(self, ev): - """ - Demultiplexes the vpn request to appropriate driver methods. - - :param ev: event object sent from the process model. - - Returns: None - """ - if ev.id == 'VPN_SYNC': - self._sc.poll_event(ev) - - if ev.id == 'VPNSERVICE_UPDATED': - try: - msg = ("Worker process with ID: %s starting " - "to handle task: %s of topic: %s. " - % (os.getpid(), - ev.id, const.VPN_GENERIC_CONFIG_RPC_TOPIC)) - LOG.debug(msg) - agent_info = ev.data['context']['agent_info'] - service_vendor = agent_info['service_vendor'] - service_feature = agent_info['service_feature'] - driver = self._get_driver(service_vendor, service_feature) - LOG.info("Invoking driver with service vendor:" - "%(service_vendor)s ", - {'service_vendor': service_vendor}) - setattr(VPNaasEventHandler, "service_driver", driver) - self._vpnservice_updated(ev, driver) - except Exception as err: - msg = ("Failed to perform the operation: %s. %s" - % (ev.id, str(err).capitalize())) - LOG.error(msg) - finally: - self._sc.event_complete(ev) - - def _vpnservice_updated(self, ev, driver): - """ - Makes call to the respective operation method of vpn driver. - - :param ev: event object sent from the process model. - :param driver: vpn driver class object. - - Returns: None. - """ - context = ev.data.get('context') - resource_data = ev.data.get('resource_data') - msg = "Vpn service updated from server side" - LOG.info(msg) - - try: - driver.vpnservice_updated(context, resource_data) - - if 'ipsec_site_conns' in context['service_info']: - for item in context['service_info']['ipsec_site_conns']: - if item['id'] == resource_data['resource']['id'] and ( - resource_data['reason'] == 'create'): - item['status'] = 'INIT' - arg_dict = {'context': context, - 'resource_data': resource_data} - ev1 = self._sc.new_event(id='VPN_SYNC', - key='VPN_SYNC', data=arg_dict) - self._sc.post_event(ev1) - break - except Exception as err: - msg = ("Failed to update VPN service. %s" - % str(err).capitalize()) - LOG.error(msg) - reason = resource_data.get('reason') - rsrc = resource_data.get('rsrc_type') - - if (reason == 'delete' and rsrc == 'ipsec_site_connection'): - conn = resource_data['resource'] - resource_id = conn['id'] - self._plugin_rpc.ipsec_site_conn_deleted(context, - resource_id=resource_id) - - def _get_service_vendor(self, vpn_svc): - """ - Extracts the vendor from the description. - :param vpn_svc: vpn service operation type dictionary, - which it gets from filter library - - Returns: None - """ - svc_desc = vpn_svc['description'] - tokens = svc_desc.split(';') - vendor = tokens[5].split('=')[1] - return vendor - - def _sync_ipsec_conns(self, context, svc_context): - """ - Gets the status of the vpn service. - :param context: Dictionary of the vpn service type. - :param vendor: vendor name - :param svc_context: vpn service operation type dictionary, - which it gets filter library - - Returns: None - """ - try: - return self.service_driver.check_status(context, svc_context) - except Exception as err: - msg = ("Failed to sync ipsec connection information. %s." - % str(err).capitalize()) - LOG.error(msg) - - @nfp_api.poll_event_desc(event='VPN_SYNC', spacing=10) - def sync(self, ev): - """Periodically updates the status of vpn service, whether the - tunnel is UP or DOWN. - - :param context: Dictionary of the vpn service type. - - Returns: None - """ - - context = ev.data.get('context') - s2s_contexts = self._plugin_rpc.get_vpn_servicecontext(context) - state = self._sync_ipsec_conns(context, s2s_contexts[0]) - if state in {const.STATE_ACTIVE, - const.STATE_ERROR}: - return {'poll': False} - - -def events_init(sc, drivers): - """Registers events with core service controller. - - All the events will come to handle_event method of class instance - registered in 'handler' field. - - :param sc: Object of Service Controller from the process model to regiters - the different events - :param drivers: Driver instance registered with the service agent - - Returns: None - """ - evs = [ - nfp_event.Event(id='VPNSERVICE_UPDATED', - handler=VPNaasEventHandler(sc, drivers)), - nfp_event.Event(id='VPN_SYNC', - handler=VPNaasEventHandler(sc, drivers))] - - sc.register_events(evs) - - -def load_drivers(sc, conf): - """Loads the drivers dynamically. - - Loads the drivers that register with the agents. - :param sc: Object of the Service Controller class from core - service controller. - - Returns: dictionary of instances of the respective driver classes. - """ - - ld = utils.ConfiguratorUtils(conf) - drivers = ld.load_drivers(const.SERVICE_TYPE) - - for service_type, driver_name in six.iteritems(drivers): - driver_obj = driver_name(conf=conf) - drivers[service_type] = driver_obj - - return drivers - - -def register_service_agent(cm, sc, conf): - """Registers the agents with Cofigurator module. - Puts all the agents into the dictionary with their service types. - :prarm cm: Configurator module's object to communicate back and forth - :param sc: Object of the Service Controller class from core - service controller. - :param conf: Object of oslo configurator passed from the core service - controller - - Returns: None - """ - - rpcmgr = VPNaasRpcManager(conf, sc) - cm.register_service_agent(const.SERVICE_TYPE, rpcmgr) - - -def init_agent(cm, sc, conf): - """Loads the drivers and registers the agents. - Loads the dynamicaaly both the drivers and agents, registers the agents - with their service types. - - :prarm cm: Configurator module's object to communicate back and forth - :param sc: Object of the Service Controller class from core - service controller. - :param conf: Object of oslo configurator passed from the core service - controller - - Returns: None - - """ - try: - drivers = load_drivers(sc, conf) - except Exception as err: - msg = ("VPNaas failed to load drivers. %s" - % (str(err).capitalize())) - LOG.error(msg) - raise err - else: - msg = "VPNaas loaded drivers successfully." - LOG.debug(msg) - - try: - events_init(sc, drivers) - except Exception as err: - msg = ("VPNaas Events initialization unsuccessful. %s" - % (str(err).capitalize())) - LOG.error(msg) - raise err - else: - msg = "VPNaas Events initialization successful." - LOG.debug(msg) - - try: - register_service_agent(cm, sc, conf) - bdobj = base_driver.BaseDriver(conf) - bdobj.register_agent_object_with_driver('agent', VpnaasRpcSender(sc)) - except Exception as err: - msg = ("VPNaas service agent registration unsuccessful. %s" - % (str(err).capitalize())) - LOG.error(msg) - raise err - else: - msg = "VPNaas service agent registration successful." - LOG.debug(msg) - - msg = "VPN as a Service Module Initialized." - LOG.info(msg) - - -def init_agent_complete(cm, sc, conf): - """ - Initializes periodic tasks. - """ - msg = " vpn agent init complete" - LOG.info(msg) diff --git a/gbpservice/contrib/nfp/configurator/drivers/__init__.py b/gbpservice/contrib/nfp/configurator/drivers/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/configurator/drivers/base/__init__.py b/gbpservice/contrib/nfp/configurator/drivers/base/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/configurator/drivers/base/base_driver.py b/gbpservice/contrib/nfp/configurator/drivers/base/base_driver.py deleted file mode 100644 index a0a7d9e47..000000000 --- a/gbpservice/contrib/nfp/configurator/drivers/base/base_driver.py +++ /dev/null @@ -1,167 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import subprocess - -from oslo_serialization import jsonutils -import requests - -from gbpservice.contrib.nfp.configurator.lib import constants as const -from gbpservice.nfp.core import log as nfp_logging - -LOG = nfp_logging.getLogger(__name__) - - -def set_class_attr(**kwargs): - def f(class_obj): - for key, value in list(kwargs.items()): - setattr(class_obj, key.lower(), value.lower()) - return class_obj - return f - - -class BaseDriver(object): - """ Implements common functions for drivers. - - Every service vendor must inherit this class. If any service vendor wants - to add extra methods for their service, apart from below given, they should - add method definition here and implement the method in their driver - """ - - def __init__(self, conf): - pass - - def configure_healthmonitor(self, context, resource_data): - """Checks if the Service VM is reachable. - - It does netcat to the CONFIGURATION_SERVER_PORT of the Service VM. - Configuration agent runs inside Service VM. Once agent is up and - reachable, Service VM is assumed to be active. - - :param context - context - :param resource_data - data coming from orchestrator - - Returns: SUCCESS/FAILED - - """ - - resource_data = self.parse.parse_data(const.HEALTHMONITOR, - resource_data) - ip = resource_data.get('mgmt_ip') - port = str(self.port) - command = 'nc ' + ip + ' ' + port + ' -z' - return self._check_vm_health(command) - - def configure_interfaces(self, context, kwargs): - return const.SUCCESS - - def clear_interfaces(self, context, kwargs): - return const.SUCCESS - - def configure_routes(self, context, kwargs): - return const.SUCCESS - - def clear_routes(self, context, kwargs): - return const.SUCCESS - - def clear_healthmonitor(self, context, kwargs): - return const.SUCCESS - - def register_agent_object_with_driver(self, name, agent_obj): - setattr(BaseDriver, name, agent_obj) - - def _check_vm_health(self, command): - """Ping based basic HM support provided by BaseDriver. - Service provider can override the method implementation - if they want to support other types. - - :param command - command to execute - - Returns: SUCCESS/FAILED - """ - msg = ("Executing command %s for VM health check" % (command)) - LOG.debug(msg) - try: - subprocess.check_output(command, stderr=subprocess.STDOUT, - shell=True) - except Exception as e: - msg = ("VM health check failed. Command '%s' execution failed." - " Reason=%s" % (command, e)) - LOG.debug(msg) - return const.FAILED - return const.SUCCESS - - def _configure_log_forwarding(self, url, mgmt_ip, port, headers=None): - """ Configures log forwarding IP address in Service VMs. - - :param url: url format that is used to invoke the Service VM API - :param mgmt_ip: management IP of the Service VM - :param port: port that is listened to by the Service VM agent - - Returns: SUCCESS/Error msg - - """ - - url = url % (mgmt_ip, port, 'configure-rsyslog-as-client') - - log_forward_ip_address = self.conf.configurator.log_forward_ip_address - if not log_forward_ip_address: - msg = ("Log forwarding IP address not configured " - "for service at %s." % mgmt_ip) - LOG.info(msg) - return const.UNHANDLED - - data = dict( - server_ip=log_forward_ip_address, - server_port=self.conf.configurator.log_forward_port, - log_level=self.conf.configurator.log_level) - data = jsonutils.dumps(data) - - msg = ("Initiating POST request to configure log forwarding " - "for service at: %r" % mgmt_ip) - LOG.info(msg) - - try: - resp = requests.post(url, data=data, - timeout=self.timeout, headers=headers) - except requests.exceptions.ConnectionError as err: - msg = ("Failed to establish connection to service at: " - "%r for configuring log forwarding. ERROR: %r" % - (mgmt_ip, str(err).capitalize())) - LOG.error(msg) - return msg - except requests.exceptions.RequestException as err: - msg = ("Unexpected ERROR happened while configuring " - "log forwarding for service at: %r. " - "ERROR: %r" % - (mgmt_ip, str(err).capitalize())) - LOG.error(msg) - return msg - - try: - result = resp.json() - except ValueError as err: - msg = ("Unable to parse response of configure log forward API, " - "invalid JSON. URL: %r. %r" % (url, str(err).capitalize())) - LOG.error(msg) - return msg - if not result['status']: - msg = ("Error configuring log forwarding for service " - "at %s. URL: %r. Reason: %s." % - (mgmt_ip, url, result['reason'])) - LOG.error(msg) - return msg - - msg = ("Successfully configured log forwarding for " - "service at %s." % mgmt_ip) - LOG.info(msg) - return const.SUCCESS diff --git a/gbpservice/contrib/nfp/configurator/drivers/firewall/__init__.py b/gbpservice/contrib/nfp/configurator/drivers/firewall/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/configurator/drivers/firewall/vyos/__init__.py b/gbpservice/contrib/nfp/configurator/drivers/firewall/vyos/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/configurator/drivers/firewall/vyos/vyos_fw_constants.py b/gbpservice/contrib/nfp/configurator/drivers/firewall/vyos/vyos_fw_constants.py deleted file mode 100644 index 1ab2aeb49..000000000 --- a/gbpservice/contrib/nfp/configurator/drivers/firewall/vyos/vyos_fw_constants.py +++ /dev/null @@ -1,18 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -VYOS = 'vyos' -CONFIGURATION_SERVER_PORT = '8888' -REST_TIMEOUT = 180 -request_url = "http://%s:%s/%s" - -INTERFACE_NOT_FOUND = "INTERFACE NOT FOUND" diff --git a/gbpservice/contrib/nfp/configurator/drivers/firewall/vyos/vyos_fw_driver.py b/gbpservice/contrib/nfp/configurator/drivers/firewall/vyos/vyos_fw_driver.py deleted file mode 100644 index 17de72b0a..000000000 --- a/gbpservice/contrib/nfp/configurator/drivers/firewall/vyos/vyos_fw_driver.py +++ /dev/null @@ -1,695 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import time - -from oslo_serialization import jsonutils -import requests - -from gbpservice.contrib.nfp.configurator.drivers.base import base_driver -from gbpservice.contrib.nfp.configurator.drivers.firewall.vyos import ( - vyos_fw_constants as const) -from gbpservice.contrib.nfp.configurator.lib import ( - generic_config_constants as gen_cfg_const) -from gbpservice.contrib.nfp.configurator.lib import constants as common_const -from gbpservice.contrib.nfp.configurator.lib import data_parser -from gbpservice.contrib.nfp.configurator.lib import fw_constants as fw_const -from gbpservice.nfp.core import log as nfp_logging - -LOG = nfp_logging.getLogger(__name__) - - -class RestApi(object): - """ Issues REST calls to the Service VMs - - REST API wrapper class that provides POST method to - communicate with the Service VM. - - """ - - def __init__(self, timeout): - self.timeout = timeout - - def request_type_to_api_map(self, url, data, request_type, headers): - return getattr(requests, request_type)(url, - data=data, timeout=self.timeout, - headers=headers) - - def fire(self, url, data, request_type, headers): - """ Invokes REST POST call to the Service VM. - - :param url: URL to connect. - :param data: data to be sent. - :param request_type: POST/PUT/DELETE - - Returns: SUCCESS/Error message - - """ - - try: - msg = ("SENDING CURL request to URL: %s, request_type:%s, " - "vm with data %s" - % (url, request_type, data)) - LOG.debug(msg) - resp = self.request_type_to_api_map(url, data, - request_type.lower(), headers) - except requests.exceptions.ConnectionError as err: - msg = ("Failed to establish connection to the service at URL: %r. " - "ERROR: %r" % (url, str(err).capitalize())) - return msg - except Exception as err: - msg = ("Failed to issue %r call " - "to service. URL: %r, Data: %r. Error: %r" % - (request_type.upper(), url, data, str(err).capitalize())) - return msg - - try: - result = resp.json() - except ValueError as err: - msg = ("Unable to parse response, invalid JSON. URL: " - "%r. %r" % (url, str(err).capitalize())) - return msg - if resp.status_code not in common_const.SUCCESS_CODES or ( - result.get('status') is False): - return result - return common_const.STATUS_SUCCESS - - -class FwGenericConfigDriver(base_driver.BaseDriver): - """ Implements device configuration requests. - - Firewall generic configuration driver for handling device - configuration requests from Orchestrator. - """ - - def __init__(self): - self.parse = data_parser.DataParser() - - def _parse_vm_context(self, context): - try: - username = str(context['service_vm_context'][ - 'vyos']['username']) - password = str(context['service_vm_context'][ - 'vyos']['password']) - headers = {'Content-Type': 'application/json', - 'username': username, - 'password': password} - return headers - except Exception as e: - msg = ("Failed to get header from context. ERROR: %s" % e) - LOG.error(msg) - raise Exception(msg) - - def configure_healthmonitor(self, context, resource_data): - vm_status = super(FwGenericConfigDriver, self).configure_healthmonitor( - context, resource_data) - if resource_data['nfds'][0]['periodicity'] == gen_cfg_const.INITIAL: - if vm_status == common_const.SUCCESS: - try: - resp = self.configure_user(context, resource_data) - if resp != common_const.STATUS_SUCCESS: - return common_const.FAILURE - except Exception as e: - msg = ("Failed to configure user. ERROR: %s" % e) - LOG.error(msg) - return common_const.FAILURE - return vm_status - - def configure_user(self, context, resource_data): - headers = self._parse_vm_context(context) - resource_data = self.parse.parse_data(common_const.HEALTHMONITOR, - resource_data) - mgmt_ip = resource_data['mgmt_ip'] - url = const.request_url % (mgmt_ip, - self.port, - 'change_auth') - data = {} - LOG.info("Initiating POST request to configure Authentication " - "service at mgmt ip:%(mgmt_ip)s", - {'mgmt_ip': mgmt_ip}) - err_msg = ("Change Auth POST request to the VyOS firewall " - "service at %s failed. " % url) - try: - resp = self.rest_api.fire(url, data, common_const.POST, headers) - except Exception as err: - err_msg += ("Reason: %r" % str(err).capitalize()) - LOG.error(err_msg) - return err_msg - - if resp is common_const.STATUS_SUCCESS: - msg = ("Configured user authentication successfully" - " for vyos service at %r." % mgmt_ip) - LOG.info(msg) - return resp - - err_msg += (("Failed to change Authentication para Status code " - "Status code: %r, Reason: %r" % - (resp['status'], resp['reason'])) - if type(resp) is dict - else ("Reason: " + resp)) - LOG.error(err_msg) - return err_msg - - def _configure_static_ips(self, context, resource_data): - """ Configure static IPs for provider and stitching interfaces - of service VM. - - Issues REST call to service VM for configuration of static IPs. - - :param resource_data: a dictionary of firewall rules and objects - send by neutron plugin - - Returns: SUCCESS/Failure message with reason. - - """ - headers = self._parse_vm_context(context) - static_ips_info = dict( - provider_ip=resource_data.get('provider_ip'), - provider_cidr=resource_data.get('provider_cidr'), - provider_mac=resource_data.get('provider_mac'), - stitching_ip=resource_data.get('stitching_ip'), - stitching_cidr=resource_data.get('stitching_cidr'), - stitching_mac=resource_data.get('stitching_mac')) - mgmt_ip = resource_data['mgmt_ip'] - - url = const.request_url % (mgmt_ip, - self.port, - 'add_static_ip') - data = jsonutils.dumps(static_ips_info) - - LOG.info("Initiating POST request to add static IPs for primary " - "service at mgmt ip:%(mgmt_ip)s", - {'mgmt_ip': mgmt_ip}) - err_msg = ("Static IP POST request to the VyOS firewall " - "service at %s failed. " % url) - try: - resp = self.rest_api.fire(url, data, common_const.POST, headers) - except Exception as err: - err_msg += ("Reason: %r" % str(err).capitalize()) - LOG.error(err_msg) - return err_msg - - if resp is common_const.STATUS_SUCCESS: - msg = ("Static IPs successfully added for service at %r." % url) - LOG.info(msg) - return resp - - err_msg += (("Status code: %r, Reason: %r" % - (resp['status'], resp['reason'])) - if type(resp) is dict - else ("Reason: " + resp)) - LOG.error(err_msg) - return err_msg - - def configure_interfaces(self, context, resource_data): - """ Configure interfaces for the service VM. - - Calls static IP configuration function and implements - persistent rule addition in the service VM. - Issues REST call to service VM for configuration of interfaces. - - :param context: neutron context - :param resource_data: a dictionary of firewall rules and objects - send by neutron plugin - - Returns: SUCCESS/Failure message with reason. - - """ - headers = self._parse_vm_context(context) - resource_data = self.parse.parse_data(common_const.INTERFACES, - resource_data) - mgmt_ip = resource_data['mgmt_ip'] - - try: - result_log_forward = self._configure_log_forwarding( - const.request_url, mgmt_ip, self.port, headers) - except Exception as err: - msg = ("Failed to configure log forwarding for service at %s. " - "Error: %s" % (mgmt_ip, err)) - LOG.error(msg) - else: - if result_log_forward == common_const.UNHANDLED: - pass - elif result_log_forward != common_const.STATUS_SUCCESS: - # Failure in log forward configuration won't break chain - # creation. However, error will be logged for detecting - # failure. - msg = ("Failed to configure log forwarding for service at %s." - " Error: %s" % (mgmt_ip, result_log_forward)) - LOG.error(msg) - - try: - result_static_ips = self._configure_static_ips(context, - resource_data) - except Exception as err: - msg = ("Failed to add static IPs. Error: %s" % err) - LOG.error(msg) - return msg - else: - if result_static_ips != common_const.STATUS_SUCCESS: - return result_static_ips - - rule_info = dict( - provider_mac=resource_data['provider_mac'], - stitching_mac=resource_data['stitching_mac']) - - url = const.request_url % (mgmt_ip, - self.port, 'add_rule') - data = jsonutils.dumps(rule_info) - LOG.info("Initiating POST request to add persistent rule to " - "primary service at mgmt ip: %(mgmt_ip)s", - {'mgmt_ip': mgmt_ip}) - err_msg = ("Add persistent rule POST request to the VyOS firewall " - "service at %s failed. " % url) - try: - resp = self.rest_api.fire(url, data, common_const.POST, headers) - except Exception as err: - err_msg += ("Reason: %r" % str(err).capitalize()) - LOG.error(err_msg) - return err_msg - - if resp is common_const.STATUS_SUCCESS: - msg = ("Persistent rule successfully added for " - "service at %r." % url) - LOG.info(msg) - - # wait for 10secs for the ip address to get configured. Sometimes - # observed that 'set_routes' fail with 'ip not configured' error. - time.sleep(10) - return resp - - err_msg += (("Status code: %r" % resp['status']) - if type(resp) is dict - else ("Reason: " + resp)) - LOG.error(err_msg) - return err_msg - - def _clear_static_ips(self, context, resource_data): - """ Clear static IPs for provider and stitching - interfaces of the service VM. - - Issues REST call to service VM for deletion of static IPs. - - :param resource_data: a dictionary of firewall rules and objects - send by neutron plugin - - Returns: SUCCESS/Failure message with reason. - - """ - headers = self._parse_vm_context(context) - static_ips_info = dict( - provider_ip=resource_data.get('provider_ip'), - provider_cidr=resource_data.get('provider_cidr'), - provider_mac=resource_data.get('provider_mac'), - stitching_ip=resource_data.get('stitching_ip'), - stitching_cidr=resource_data.get('stitching_cidr'), - stitching_mac=resource_data.get('stitching_mac')) - mgmt_ip = resource_data['mgmt_ip'] - - url = const.request_url % (mgmt_ip, - self.port, - 'del_static_ip') - data = jsonutils.dumps(static_ips_info) - - LOG.info("Initiating POST request to remove static IPs for " - "primary service at mgmt ip: %(mgmt_ip)s", - {'mgmt_ip': mgmt_ip}) - - err_msg = ("Static IP DELETE request to the VyOS firewall " - "service at %s failed. " % url) - try: - resp = self.rest_api.fire(url, data, common_const.DELETE, headers) - except Exception as err: - err_msg += ("Reason: %r" % str(err).capitalize()) - LOG.error(err_msg) - return err_msg - - if resp is common_const.STATUS_SUCCESS: - msg = ("Static IPs successfully removed for service at %r." % url) - LOG.info(msg) - return resp - - err_msg += (("Status code: %r, Reason: %r" % - (resp['status'], resp['reason'])) - if type(resp) is dict - else ("Reason: " + resp)) - LOG.error(err_msg) - return err_msg - - def clear_interfaces(self, context, resource_data): - """ Clear interfaces for the service VM. - - Calls static IP clear function and implements - persistent rule deletion in the service VM. - Issues REST call to service VM for deletion of interfaces. - - :param context: neutron context - :param resource_data: a dictionary of firewall rules and objects - send by neutron plugin - - Returns: SUCCESS/Failure message with reason. - - """ - headers = self._parse_vm_context(context) - resource_data = self.parse.parse_data(common_const.INTERFACES, - resource_data) - try: - result_static_ips = self._clear_static_ips(context, resource_data) - except Exception as err: - msg = ("Failed to remove static IPs. Error: %s" % err) - LOG.error(msg) - return msg - else: - if result_static_ips != common_const.STATUS_SUCCESS: - return result_static_ips - else: - LOG.info("Successfully removed static IPs. " - "Result: %(result_static_ips)s", - {'result_static_ips': result_static_ips}) - - rule_info = dict( - provider_mac=resource_data['provider_mac'], - stitching_mac=resource_data['stitching_mac']) - - mgmt_ip = resource_data['mgmt_ip'] - - LOG.info("Initiating DELETE persistent rule for primary " - "service at mgmt ip: %(mgmt_ip)s", - {'mgmt_ip': mgmt_ip}) - url = const.request_url % (mgmt_ip, self.port, 'delete_rule') - data = jsonutils.dumps(rule_info) - - err_msg = ("Persistent rule DELETE request to the VyOS firewall " - "service at %s failed. " % url) - try: - resp = self.rest_api.fire(url, data, common_const.DELETE, headers) - except Exception as err: - err_msg += ("Reason: %r" % str(err).capitalize()) - LOG.error(err_msg) - return err_msg - - if resp is common_const.STATUS_SUCCESS: - msg = ("Persistent rules successfully deleted " - "for service at %r." % url) - LOG.info(msg) - return resp - - err_msg += (("Status code: %r." % resp['status']) - if type(resp) is dict - else ("Reason: " + resp)) - LOG.error(err_msg) - return err_msg - - def configure_routes(self, context, resource_data): - """ Configure routes for the service VM. - - Issues REST call to service VM for configuration of routes. - - :param context: neutron context - :param resource_data: a dictionary of firewall rules and objects - send by neutron plugin - - Returns: SUCCESS/Failure message with reason. - - """ - headers = self._parse_vm_context(context) - forward_routes = resource_data.get('forward_route') - resource_data = self.parse.parse_data(common_const.ROUTES, - resource_data) - mgmt_ip = resource_data.get('mgmt_ip') - gateway_ip = resource_data.get('stitching_gw_ip') - - # checking whether VPN service is present in the chain - # if yes, just configure the stitching pbr else - # configure both stitching and provider pbrs. - - if not forward_routes: - source_cidrs = [resource_data.get('stitching_cidr')] - else: - source_cidrs = [resource_data.get('provider_cidr'), - resource_data.get('stitching_cidr')] - - url = const.request_url % (mgmt_ip, self.port, - 'add-source-route') - route_info = [] - for source_cidr in source_cidrs: - route_info.append({'source_cidr': source_cidr, - 'gateway_ip': gateway_ip}) - data = jsonutils.dumps(route_info) - LOG.info("Initiating POST request to configure route of primary " - "service at mgmt ip: %(mgmt_ip)s", - {'mgmt_ip': mgmt_ip}) - - err_msg = ("Configure routes POST request to the VyOS firewall " - "service at %s failed. " % url) - try: - resp = self.rest_api.fire(url, data, common_const.POST, headers) - except Exception as err: - err_msg += ("Reason: %r" % str(err).capitalize()) - LOG.error(err_msg) - return err_msg - - if resp is common_const.STATUS_SUCCESS: - msg = ("Configured routes successfully for service at %r." % url) - LOG.info(msg) - return resp - - err_msg += (("Status code: %r, Reason: %r" % - (resp['status'], resp['reason'])) - if type(resp) is dict - else ("Reason: " + resp)) - LOG.error(err_msg) - return err_msg - - def clear_routes(self, context, resource_data): - """ Clear routes for the service VM. - - Issues REST call to service VM for deletion of routes. - - :param context: neutron context - :param resource_data: a dictionary of firewall rules and objects - send by neutron plugin - - Returns: SUCCESS/Failure message with reason. - - """ - headers = self._parse_vm_context(context) - resource_data = self.parse.parse_data(common_const.ROUTES, - resource_data) - mgmt_ip = resource_data.get('mgmt_ip') - source_cidrs = [resource_data.get('provider_cidr'), - resource_data.get('stitching_cidr')] - - url = const.request_url % (mgmt_ip, self.port, - 'delete-source-route') - route_info = [] - for source_cidr in source_cidrs: - route_info.append({'source_cidr': source_cidr}) - data = jsonutils.dumps(route_info) - LOG.info("Initiating Delete route to primary " - "service at mgmt ip: %(mgmt_ip)s", - {'mgmt_ip': mgmt_ip}) - - err_msg = ("Routes DELETE request to the VyOS firewall " - "service at %s failed. " % url) - try: - resp = self.rest_api.fire(url, data, common_const.DELETE, headers) - except Exception as err: - err_msg += ("Reason: %r" % str(err).capitalize()) - LOG.error(err_msg) - return err_msg - - if resp is common_const.STATUS_SUCCESS: - msg = ("Routes successfully removed for service at %r." % url) - LOG.info(msg) - return resp - - err_msg += (("Status code: %r, Reason: %r" % - (resp['status'], resp['reason'])) - if type(resp) is dict - else ("Reason: " + resp)) - LOG.error(err_msg) - return err_msg - - -@base_driver.set_class_attr(SERVICE_TYPE=fw_const.SERVICE_TYPE, - SERVICE_VENDOR=const.VYOS) -class FwaasDriver(FwGenericConfigDriver): - """ Firewall as a service driver for handling firewall - service configuration requests. - - We initialize service type in this class because agent loads - class object only for those driver classes that have service type - initialized. Also, only this driver class is exposed to the agent. - - """ - - def __init__(self, conf): - self.conf = conf - self.timeout = const.REST_TIMEOUT - self.rest_api = RestApi(self.timeout) - self.host = self.conf.host - self.port = const.CONFIGURATION_SERVER_PORT - super(FwaasDriver, self).__init__() - - def create_firewall(self, context, firewall, host): - """ Implements firewall creation - - Issues REST call to service VM for firewall creation - - :param context: Neutron context - :param firewall: Firewall resource object from neutron fwaas plugin - :param host: Name of the host machine - - Returns: SUCCESS/Failure message with reason. - - """ - headers = self._parse_vm_context(context['agent_info']['context']) - resource_data = self.parse.parse_data(common_const.FIREWALL, context) - - LOG.info("Processing request 'Create Firewall' in FWaaS Driver " - "for Firewall ID: %(f_id)s", - {'f_id': firewall['id']}) - mgmt_ip = resource_data.get('mgmt_ip') - url = const.request_url % (mgmt_ip, - self.port, - 'configure-firewall-rule') - msg = ("Initiating POST request for FIREWALL ID: %r Tenant ID:" - " %r. URL: %s" % (firewall['id'], firewall['tenant_id'], url)) - LOG.debug(msg) - data = jsonutils.dumps(firewall) - - err_msg = ("Configure firewall POST request to the VyOS " - "service at %s failed. " % url) - try: - resp = self.rest_api.fire(url, data, common_const.POST, headers) - except Exception as err: - err_msg += ("Reason: %r" % str(err).capitalize()) - LOG.error(err_msg) - return common_const.STATUS_ERROR - - if resp is common_const.STATUS_SUCCESS: - LOG.info("Configured firewall successfully at URL: %(url)s ", - {'url': url}) - return common_const.STATUS_ACTIVE - - err_msg += (("Reason: %r, Response Content: %r" % - (resp.pop('message'), resp)) - if type(resp) is dict - else ("Reason: " + resp)) - LOG.error(err_msg) - return common_const.STATUS_ERROR - - def update_firewall(self, context, firewall, host): - """ Implements firewall updation - - Issues REST call to service VM for firewall updation - - :param context: Neutron context - :param firewall: Firewall resource object from neutron fwaas plugin - :param host: Name of the host machine - - Returns: SUCCESS/Failure message with reason. - - """ - headers = self._parse_vm_context(context['agent_info']['context']) - LOG.info("Processing request 'Update Firewall' in FWaaS Driver " - "for Firewall ID:%(f_id)s", - {'f_id': firewall['id']}) - resource_data = self.parse.parse_data(common_const.FIREWALL, context) - mgmt_ip = resource_data.get('mgmt_ip') - url = const.request_url % (mgmt_ip, - self.port, - 'update-firewall-rule') - msg = ("Initiating UPDATE request. URL: %s" % url) - LOG.debug(msg) - data = jsonutils.dumps(firewall) - - err_msg = ("Update firewall POST request to the VyOS " - "service at %s failed. " % url) - try: - resp = self.rest_api.fire(url, data, common_const.PUT, headers) - except Exception as err: - err_msg += ("Reason: %r" % str(err).capitalize()) - LOG.error(err_msg) - return common_const.STATUS_ERROR - - if resp is common_const.STATUS_SUCCESS: - msg = ("Updated firewall successfully for service at %r." % url) - LOG.debug(msg) - return common_const.STATUS_ACTIVE - - err_msg += (("Reason: %r, Response Content: %r" % - (resp.pop('message'), resp)) - if type(resp) is dict - else ("Reason: " + resp)) - LOG.error(err_msg) - return common_const.STATUS_ERROR - - def delete_firewall(self, context, firewall, host): - """ Implements firewall deletion - - Issues REST call to service VM for firewall deletion - - :param context: Neutron context - :param firewall: Firewall resource object from neutron fwaas plugin - :param host: Name of the host machine - - Returns: SUCCESS/Failure message with reason. - - """ - headers = self._parse_vm_context(context['agent_info']['context']) - LOG.info("Processing request 'Delete Firewall' in FWaaS Driver " - "for Firewall ID:%(f_id)s", - {'f_id': firewall['id']}) - resource_data = self.parse.parse_data(common_const.FIREWALL, context) - mgmt_ip = resource_data.get('mgmt_ip') - url = const.request_url % (mgmt_ip, - self.port, - 'delete-firewall-rule') - msg = ("Initiating DELETE request. URL: %s" % url) - LOG.info(msg) - data = jsonutils.dumps(firewall) - - err_msg = ("Delete firewall POST request to the VyOS " - "service at %s failed. " % url) - try: - resp = self.rest_api.fire(url, data, common_const.DELETE, headers) - except Exception as err: - err_msg += ("Reason: %r" % str(err).capitalize()) - LOG.error(err_msg) - return common_const.STATUS_SUCCESS - - if resp is common_const.STATUS_SUCCESS: - msg = ("Deleted firewall successfully for service at %r." % url) - LOG.info(msg) - return common_const.STATUS_DELETED - - if type(resp) is dict: - if not resp.get('delete_success') and ( - resp.get('message') == const.INTERFACE_NOT_FOUND): - err_msg += ("Firewall was not deleted as interface was not " - "available in the firewall. It might have got " - "detached. So marking this delete as SUCCESS. " - "URL: %r, Response Content: %r" % - (url, resp.content)) - LOG.error(err_msg) - return common_const.STATUS_SUCCESS - else: - err_msg += ("Response Content: %r" % resp) - else: - err_msg += ("Reason: " + resp) - LOG.error(err_msg) - msg = ("Firewall deletion has failed, but still sending" - "status as firewall deleted success from configurator") - LOG.info(msg) - return common_const.STATUS_DELETED diff --git a/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/__init__.py b/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/__init__.py b/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/common/neutron_lbaas_data_models.py b/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/common/neutron_lbaas_data_models.py deleted file mode 100644 index 0bf42bfea..000000000 --- a/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/common/neutron_lbaas_data_models.py +++ /dev/null @@ -1,728 +0,0 @@ -# Copyright (c) 2014 OpenStack Foundation. -# All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. -""" -This module holds the data models for the load balancer service plugin. These -are meant simply as replacement data structures for dictionaries and -SQLAlchemy models. Using dictionaries as data containers for many components -causes readability issues and does not intuitively give the benefits of what -classes and OO give. Using SQLAlchemy models as data containers for many -components can become an issue if you do not want to give certain components -access to the database. - -These data models do provide methods for instantiation from SQLAlchemy models -and also converting to dictionaries. -""" - -from gbpservice.contrib.nfp.configurator.lib import lbv2_constants as l_const - -import six -if six.PY3: - unicode = str - - -class BaseDataModel(object): - - # NOTE(ihrachys): we could reuse the list to provide a default __init__ - # implementation. That would require handling custom default values though. - fields = [] - - def to_dict(self, **kwargs): - ret = {} - for attr in self.__dict__: - if attr.startswith('_') or not kwargs.get(attr, True): - continue - if isinstance(getattr(self, attr), list): - ret[attr] = [] - for item in self.__dict__[attr]: - if isinstance(item, BaseDataModel): - ret[attr].append(item.to_dict()) - else: - ret[attr] = item - elif isinstance(getattr(self, attr), BaseDataModel): - ret[attr] = self.__dict__[attr].to_dict() - elif isinstance(self.__dict__[attr], unicode): - ret[attr.encode('utf8')] = self.__dict__[attr].encode('utf8') - else: - ret[attr] = self.__dict__[attr] - return ret - - def to_api_dict(self, **kwargs): - return {} - - @classmethod - def from_dict(cls, model_dict): - fields = {k: v for k, v in list(model_dict.items()) - if k in cls.fields} - return cls(**fields) - - @property - def root_loadbalancer(self): - """Returns the loadbalancer this instance is attached to.""" - if isinstance(self, LoadBalancer): - lb = self - elif isinstance(self, Listener): - lb = self.loadbalancer - elif isinstance(self, L7Policy): - lb = self.listener.loadbalancer - elif isinstance(self, L7Rule): - lb = self.policy.listener.loadbalancer - elif isinstance(self, Pool): - lb = self.loadbalancer - elif isinstance(self, SNI): - lb = self.listener.loadbalancer - else: - # Pool Member or Health Monitor - lb = self.pool.loadbalancer - return lb - - -# NOTE(brandon-logan) AllocationPool, HostRoute, Subnet, IPAllocation, Port, -# and ProviderResourceAssociation are defined here because there aren't any -# data_models defined in core neutron or neutron services. Instead of jumping -# through the hoops to create those I've just defined them here. If ever -# data_models or similar are defined in those packages, those should be used -# instead of these. -class AllocationPool(BaseDataModel): - - fields = ['start', 'end'] - - def __init__(self, start=None, end=None): - self.start = start - self.end = end - - -class HostRoute(BaseDataModel): - - fields = ['destination', 'nexthop'] - - def __init__(self, destination=None, nexthop=None): - self.destination = destination - self.nexthop = nexthop - - -class Subnet(BaseDataModel): - - fields = ['id', 'name', 'tenant_id', 'network_id', 'ip_version', 'cidr', - 'gateway_ip', 'enable_dhcp', 'ipv6_ra_mode', 'ipv6_address_mode', - 'shared', 'dns_nameservers', 'host_routes', 'allocation_pools', - 'subnetpool_id'] - - def __init__(self, id=None, name=None, tenant_id=None, network_id=None, - ip_version=None, cidr=None, gateway_ip=None, enable_dhcp=None, - ipv6_ra_mode=None, ipv6_address_mode=None, shared=None, - dns_nameservers=None, host_routes=None, allocation_pools=None, - subnetpool_id=None): - self.id = id - self.name = name - self.tenant_id = tenant_id - self.network_id = network_id - self.ip_version = ip_version - self.cidr = cidr - self.gateway_ip = gateway_ip - self.enable_dhcp = enable_dhcp - self.ipv6_ra_mode = ipv6_ra_mode - self.ipv6_address_mode = ipv6_address_mode - self.shared = shared - self.dns_nameservers = dns_nameservers - self.host_routes = host_routes - self.allocation_pools = allocation_pools - self.subnetpool_id = subnetpool_id - - @classmethod - def from_dict(cls, model_dict): - host_routes = model_dict.pop('host_routes', []) - allocation_pools = model_dict.pop('allocation_pools', []) - model_dict['host_routes'] = [HostRoute.from_dict(route) - for route in host_routes] - model_dict['allocation_pools'] = [AllocationPool.from_dict(ap) - for ap in allocation_pools] - return super(Subnet, cls).from_dict(model_dict) - - -class IPAllocation(BaseDataModel): - - fields = ['port_id', 'ip_address', 'subnet_id', 'network_id'] - - def __init__(self, port_id=None, ip_address=None, subnet_id=None, - network_id=None): - self.port_id = port_id - self.ip_address = ip_address - self.subnet_id = subnet_id - self.network_id = network_id - - @classmethod - def from_dict(cls, model_dict): - subnet = model_dict.pop('subnet', None) - # TODO(blogan): add subnet to __init__. Can't do it yet because it - # causes issues with converting SA models into data models. - instance = super(IPAllocation, cls).from_dict(model_dict) - setattr(instance, 'subnet', None) - if subnet: - setattr(instance, 'subnet', Subnet.from_dict(subnet)) - return instance - - -class Port(BaseDataModel): - - fields = ['id', 'tenant_id', 'name', 'network_id', 'mac_address', - 'admin_state_up', 'status', 'device_id', 'device_owner', - 'fixed_ips'] - - def __init__(self, id=None, tenant_id=None, name=None, network_id=None, - mac_address=None, admin_state_up=None, status=None, - device_id=None, device_owner=None, fixed_ips=None): - self.id = id - self.tenant_id = tenant_id - self.name = name - self.network_id = network_id - self.mac_address = mac_address - self.admin_state_up = admin_state_up - self.status = status - self.device_id = device_id - self.device_owner = device_owner - self.fixed_ips = fixed_ips or [] - - @classmethod - def from_dict(cls, model_dict): - fixed_ips = model_dict.pop('fixed_ips', []) - model_dict['fixed_ips'] = [IPAllocation.from_dict(fixed_ip) - for fixed_ip in fixed_ips] - return super(Port, cls).from_dict(model_dict) - - -class ProviderResourceAssociation(BaseDataModel): - - fields = ['provider_name', 'resource_id'] - - def __init__(self, provider_name=None, resource_id=None): - self.provider_name = provider_name - self.resource_id = resource_id - - @classmethod - def from_dict(cls, model_dict): - device_driver = model_dict.pop('device_driver', None) - instance = super(ProviderResourceAssociation, cls).from_dict( - model_dict) - setattr(instance, 'device_driver', device_driver) - return instance - - -class SessionPersistence(BaseDataModel): - - fields = ['pool_id', 'type', 'cookie_name', 'pool'] - - def __init__(self, pool_id=None, type=None, cookie_name=None, - pool=None): - self.pool_id = pool_id - self.type = type - self.cookie_name = cookie_name - self.pool = pool - - def to_api_dict(self): - return super(SessionPersistence, self).to_dict(pool=False, - pool_id=False) - - @classmethod - def from_dict(cls, model_dict): - pool = model_dict.pop('pool', None) - if pool: - model_dict['pool'] = Pool.from_dict( - pool) - return super(SessionPersistence, cls).from_dict(model_dict) - - -class LoadBalancerStatistics(BaseDataModel): - - fields = ['loadbalancer_id', 'bytes_in', 'bytes_out', 'active_connections', - 'total_connections', 'loadbalancer'] - - def __init__(self, loadbalancer_id=None, bytes_in=None, bytes_out=None, - active_connections=None, total_connections=None, - loadbalancer=None): - self.loadbalancer_id = loadbalancer_id - self.bytes_in = bytes_in - self.bytes_out = bytes_out - self.active_connections = active_connections - self.total_connections = total_connections - self.loadbalancer = loadbalancer - - def to_api_dict(self): - return super(LoadBalancerStatistics, self).to_dict( - loadbalancer_id=False, loadbalancer=False) - - -class HealthMonitor(BaseDataModel): - - fields = ['id', 'tenant_id', 'type', 'delay', 'timeout', 'max_retries', - 'http_method', 'url_path', 'expected_codes', - 'provisioning_status', 'admin_state_up', 'pool', 'name'] - - def __init__(self, id=None, tenant_id=None, type=None, delay=None, - timeout=None, max_retries=None, http_method=None, - url_path=None, expected_codes=None, provisioning_status=None, - admin_state_up=None, pool=None, name=None): - self.id = id - self.tenant_id = tenant_id - self.type = type - self.delay = delay - self.timeout = timeout - self.max_retries = max_retries - self.http_method = http_method - self.url_path = url_path - self.expected_codes = expected_codes - self.provisioning_status = provisioning_status - self.admin_state_up = admin_state_up - self.pool = pool - self.name = name - - def attached_to_loadbalancer(self): - return bool(self.pool and self.pool.loadbalancer) - - def to_api_dict(self): - ret_dict = super(HealthMonitor, self).to_dict( - provisioning_status=False, pool=False) - ret_dict['pools'] = [] - if self.pool: - ret_dict['pools'].append({'id': self.pool.id}) - if self.type in [l_const.HEALTH_MONITOR_TCP, - l_const.HEALTH_MONITOR_PING]: - ret_dict.pop('http_method') - ret_dict.pop('url_path') - ret_dict.pop('expected_codes') - return ret_dict - - @classmethod - def from_dict(cls, model_dict): - pool = model_dict.pop('pool', None) - if pool: - model_dict['pool'] = Pool.from_dict( - pool) - return super(HealthMonitor, cls).from_dict(model_dict) - - -class Pool(BaseDataModel): - - fields = ['id', 'tenant_id', 'name', 'description', 'healthmonitor_id', - 'protocol', 'lb_algorithm', 'admin_state_up', 'operating_status', - 'provisioning_status', 'members', 'healthmonitor', - 'session_persistence', 'loadbalancer_id', 'loadbalancer', - 'listener', 'listeners', 'l7_policies'] - - # Map deprecated attribute names to new ones. - attr_mapping = {'sessionpersistence': 'session_persistence'} - - def __init__(self, id=None, tenant_id=None, name=None, description=None, - healthmonitor_id=None, protocol=None, lb_algorithm=None, - admin_state_up=None, operating_status=None, - provisioning_status=None, members=None, healthmonitor=None, - session_persistence=None, loadbalancer_id=None, - loadbalancer=None, listener=None, listeners=None, - l7_policies=None): - self.id = id - self.tenant_id = tenant_id - self.name = name - self.description = description - self.healthmonitor_id = healthmonitor_id - self.protocol = protocol - self.lb_algorithm = lb_algorithm - self.admin_state_up = admin_state_up - self.operating_status = operating_status - self.provisioning_status = provisioning_status - self.members = members or [] - self.healthmonitor = healthmonitor - self.session_persistence = session_persistence - # NOTE(eezhova): Old attribute name is kept for backwards - # compatibility with out-of-tree drivers. - self.sessionpersistence = self.session_persistence - self.loadbalancer_id = loadbalancer_id - self.loadbalancer = loadbalancer - self.listener = listener - self.listeners = listeners or [] - self.l7_policies = l7_policies or [] - - def attached_to_loadbalancer(self): - return bool(self.loadbalancer) - - def to_api_dict(self): - ret_dict = super(Pool, self).to_dict( - provisioning_status=False, operating_status=False, - healthmonitor=False, session_persistence=False, - loadbalancer_id=False, loadbalancer=False, listener_id=False) - ret_dict['loadbalancers'] = [] - if self.loadbalancer: - ret_dict['loadbalancers'].append({'id': self.loadbalancer.id}) - ret_dict['session_persistence'] = None - if self.session_persistence: - ret_dict['session_persistence'] = ( - self.session_persistence.to_api_dict()) - ret_dict['members'] = [{'id': member.id} for member in self.members] - ret_dict['listeners'] = [{'id': listener.id} - for listener in self.listeners] - if self.listener: - ret_dict['listener_id'] = self.listener.id - else: - ret_dict['listener_id'] = None - ret_dict['l7_policies'] = [{'id': l7_policy.id} - for l7_policy in self.l7_policies] - return ret_dict - - @classmethod - def from_dict(cls, model_dict): - healthmonitor = model_dict.pop('healthmonitor', None) - session_persistence = model_dict.pop('session_persistence', None) - model_dict.pop('sessionpersistence', None) - loadbalancer = model_dict.pop('loadbalancer', None) - members = model_dict.pop('members', []) - model_dict['members'] = [Member.from_dict(member) - for member in members] - listeners = model_dict.pop('listeners', []) - model_dict['listeners'] = [Listener.from_dict(listener) - for listener in listeners] - l7_policies = model_dict.pop('l7_policies', []) - model_dict['l7_policies'] = [L7Policy.from_dict(policy) - for policy in l7_policies] - - # handle old attribute for out of tree drivers - listener = model_dict.pop('listener', None) - if listener: - model_dict['listener'] = Listener.from_dict(listener) - - if healthmonitor: - model_dict['healthmonitor'] = HealthMonitor.from_dict( - healthmonitor) - if session_persistence: - model_dict['session_persistence'] = SessionPersistence.from_dict( - session_persistence) - if loadbalancer: - model_dict['loadbalancer'] = LoadBalancer.from_dict(loadbalancer) - return super(Pool, cls).from_dict(model_dict) - - -class Member(BaseDataModel): - - fields = ['id', 'tenant_id', 'pool_id', 'address', 'protocol_port', - 'weight', 'admin_state_up', 'subnet_id', 'operating_status', - 'provisioning_status', 'pool', 'name'] - - def __init__(self, id=None, tenant_id=None, pool_id=None, address=None, - protocol_port=None, weight=None, admin_state_up=None, - subnet_id=None, operating_status=None, - provisioning_status=None, pool=None, name=None): - self.id = id - self.tenant_id = tenant_id - self.pool_id = pool_id - self.address = address - self.protocol_port = protocol_port - self.weight = weight - self.admin_state_up = admin_state_up - self.subnet_id = subnet_id - self.operating_status = operating_status - self.provisioning_status = provisioning_status - self.pool = pool - self.name = name - - def attached_to_loadbalancer(self): - return bool(self.pool and self.pool.loadbalancer) - - def to_api_dict(self): - return super(Member, self).to_dict( - provisioning_status=False, operating_status=False, pool=False) - - @classmethod - def from_dict(cls, model_dict): - pool = model_dict.pop('pool', None) - if pool: - model_dict['pool'] = Pool.from_dict( - pool) - return super(Member, cls).from_dict(model_dict) - - -class SNI(BaseDataModel): - - fields = ['listener_id', 'tls_container_id', 'position', 'listener'] - - def __init__(self, listener_id=None, tls_container_id=None, - position=None, listener=None): - self.listener_id = listener_id - self.tls_container_id = tls_container_id - self.position = position - self.listener = listener - - def attached_to_loadbalancer(self): - return bool(self.listener and self.listener.loadbalancer) - - def to_api_dict(self): - return super(SNI, self).to_dict(listener=False) - - -class TLSContainer(BaseDataModel): - - fields = ['id', 'certificate', 'private_key', 'passphrase', - 'intermediates', 'primary_cn'] - - def __init__(self, id=None, certificate=None, private_key=None, - passphrase=None, intermediates=None, primary_cn=None): - self.id = id - self.certificate = certificate - self.private_key = private_key - self.passphrase = passphrase - self.intermediates = intermediates - self.primary_cn = primary_cn - - -class L7Rule(BaseDataModel): - - fields = ['id', 'tenant_id', 'l7policy_id', 'type', 'compare_type', - 'invert', 'key', 'value', 'provisioning_status', - 'admin_state_up', 'policy'] - - def __init__(self, id=None, tenant_id=None, - l7policy_id=None, type=None, compare_type=None, invert=None, - key=None, value=None, provisioning_status=None, - admin_state_up=None, policy=None): - self.id = id - self.tenant_id = tenant_id - self.l7policy_id = l7policy_id - self.type = type - self.compare_type = compare_type - self.invert = invert - self.key = key - self.value = value - self.provisioning_status = provisioning_status - self.admin_state_up = admin_state_up - self.policy = policy - - def attached_to_loadbalancer(self): - return bool(self.policy.listener.loadbalancer) - - def to_api_dict(self): - ret_dict = super(L7Rule, self).to_dict( - provisioning_status=False, - policy=False, l7policy_id=False) - ret_dict['policies'] = [] - if self.policy: - ret_dict['policies'].append({'id': self.policy.id}) - return ret_dict - - @classmethod - def from_dict(cls, model_dict): - policy = model_dict.pop('policy', None) - if policy: - model_dict['policy'] = L7Policy.from_dict(policy) - return super(L7Rule, cls).from_dict(model_dict) - - -class L7Policy(BaseDataModel): - - fields = ['id', 'tenant_id', 'name', 'description', 'listener_id', - 'action', 'redirect_pool_id', 'redirect_url', 'position', - 'admin_state_up', 'provisioning_status', 'listener', 'rules', - 'redirect_pool'] - - def __init__(self, id=None, tenant_id=None, name=None, description=None, - listener_id=None, action=None, redirect_pool_id=None, - redirect_url=None, position=None, - admin_state_up=None, provisioning_status=None, - listener=None, rules=None, redirect_pool=None): - self.id = id - self.tenant_id = tenant_id - self.name = name - self.description = description - self.listener_id = listener_id - self.action = action - self.redirect_pool_id = redirect_pool_id - self.redirect_pool = redirect_pool - self.redirect_url = redirect_url - self.position = position - self.admin_state_up = admin_state_up - self.provisioning_status = provisioning_status - self.listener = listener - self.rules = rules or [] - - def attached_to_loadbalancer(self): - return bool(self.listener.loadbalancer) - - def to_api_dict(self): - ret_dict = super(L7Policy, self).to_dict( - listener=False, listener_id=False, - provisioning_status=False, redirect_pool=False) - ret_dict['listeners'] = [] - if self.listener: - ret_dict['listeners'].append({'id': self.listener.id}) - ret_dict['rules'] = [{'id': rule.id} for rule in self.rules] - return ret_dict - - @classmethod - def from_dict(cls, model_dict): - listener = model_dict.pop('listener', None) - redirect_pool = model_dict.pop('redirect_pool', None) - rules = model_dict.pop('rules', []) - if listener: - model_dict['listener'] = Listener.from_dict(listener) - if redirect_pool: - model_dict['redirect_pool'] = Pool.from_dict(redirect_pool) - model_dict['rules'] = [L7Rule.from_dict(rule) - for rule in rules] - return super(L7Policy, cls).from_dict(model_dict) - - -class Listener(BaseDataModel): - - fields = ['id', 'tenant_id', 'name', 'description', 'default_pool_id', - 'loadbalancer_id', 'protocol', 'default_tls_container_id', - 'sni_containers', 'protocol_port', 'connection_limit', - 'admin_state_up', 'provisioning_status', 'operating_status', - 'default_pool', 'loadbalancer', 'l7_policies'] - - def __init__(self, id=None, tenant_id=None, name=None, description=None, - default_pool_id=None, loadbalancer_id=None, protocol=None, - default_tls_container_id=None, sni_containers=None, - protocol_port=None, connection_limit=None, - admin_state_up=None, provisioning_status=None, - operating_status=None, default_pool=None, loadbalancer=None, - l7_policies=None): - self.id = id - self.tenant_id = tenant_id - self.name = name - self.description = description - self.default_pool_id = default_pool_id - self.loadbalancer_id = loadbalancer_id - self.protocol = protocol - self.default_tls_container_id = default_tls_container_id - self.sni_containers = sni_containers or [] - self.protocol_port = protocol_port - self.connection_limit = connection_limit - self.admin_state_up = admin_state_up - self.operating_status = operating_status - self.provisioning_status = provisioning_status - self.default_pool = default_pool - self.loadbalancer = loadbalancer - self.l7_policies = l7_policies or [] - - def attached_to_loadbalancer(self): - return bool(self.loadbalancer) - - def to_api_dict(self): - ret_dict = super(Listener, self).to_dict( - loadbalancer=False, loadbalancer_id=False, default_pool=False, - operating_status=False, provisioning_status=False, - sni_containers=False) - # NOTE(blogan): Returning a list to future proof for M:N objects - # that are not yet implemented. - ret_dict['loadbalancers'] = [] - if self.loadbalancer: - ret_dict['loadbalancers'].append({'id': self.loadbalancer.id}) - ret_dict['sni_container_refs'] = [container.tls_container_id - for container in self.sni_containers] - ret_dict['default_tls_container_ref'] = self.default_tls_container_id - ret_dict['l7_policies'] = [{'id': l7_policy.id} - for l7_policy in self.l7_policies] - return ret_dict - - @classmethod - def from_dict(cls, model_dict): - default_pool = model_dict.pop('default_pool', None) - loadbalancer = model_dict.pop('loadbalancer', None) - sni_containers = model_dict.pop('sni_containers', []) - model_dict['sni_containers'] = [SNI.from_dict(sni) - for sni in sni_containers] - l7_policies = model_dict.pop('l7_policies', []) - if default_pool: - model_dict['default_pool'] = Pool.from_dict(default_pool) - if loadbalancer: - model_dict['loadbalancer'] = LoadBalancer.from_dict(loadbalancer) - model_dict['l7_policies'] = [L7Policy.from_dict(policy) - for policy in l7_policies] - return super(Listener, cls).from_dict(model_dict) - - -class LoadBalancer(BaseDataModel): - - fields = ['id', 'tenant_id', 'name', 'description', 'vip_subnet_id', - 'vip_port_id', 'vip_address', 'provisioning_status', - 'operating_status', 'admin_state_up', 'vip_port', 'stats', - 'provider', 'listeners', 'pools', 'flavor_id'] - - def __init__(self, id=None, tenant_id=None, name=None, description=None, - vip_subnet_id=None, vip_port_id=None, vip_address=None, - provisioning_status=None, operating_status=None, - admin_state_up=None, vip_port=None, stats=None, - provider=None, listeners=None, pools=None, flavor_id=None): - self.id = id - self.tenant_id = tenant_id - self.name = name - self.description = description - self.vip_subnet_id = vip_subnet_id - self.vip_port_id = vip_port_id - self.vip_address = vip_address - self.operating_status = operating_status - self.provisioning_status = provisioning_status - self.admin_state_up = admin_state_up - self.vip_port = vip_port - self.stats = stats - self.provider = provider - self.listeners = listeners or [] - self.flavor_id = flavor_id - self.pools = pools or [] - - def attached_to_loadbalancer(self): - return True - - def to_api_dict(self): - ret_dict = super(LoadBalancer, self).to_dict( - vip_port=False, stats=False, listeners=False) - ret_dict['listeners'] = [{'id': listener.id} - for listener in self.listeners] - ret_dict['pools'] = [{'id': pool.id} for pool in self.pools] - if self.provider: - ret_dict['provider'] = self.provider.provider_name - - if not self.flavor_id: - del ret_dict['flavor_id'] - - return ret_dict - - @classmethod - def from_dict(cls, model_dict): - listeners = model_dict.pop('listeners', []) - pools = model_dict.pop('pools', []) - vip_port = model_dict.pop('vip_port', None) - provider = model_dict.pop('provider', None) - model_dict.pop('stats', None) - model_dict['listeners'] = [Listener.from_dict(listener) - for listener in listeners] - model_dict['pools'] = [Pool.from_dict(pool) - for pool in pools] - if vip_port: - model_dict['vip_port'] = Port.from_dict(vip_port) - if provider: - model_dict['provider'] = ProviderResourceAssociation.from_dict( - provider) - return super(LoadBalancer, cls).from_dict(model_dict) - - -NAME_TO_DATA_MODEL_MAP = { - "loadbalancer": LoadBalancer, - "healthmonitor": HealthMonitor, - "listener": Listener, - "sni": SNI, - "pool": Pool, - "member": Member, - "loadbalancerstatistics": LoadBalancerStatistics, - "sessionpersistence": SessionPersistence, - "ipallocation": IPAllocation, - "port": Port, - "providerresourceassociation": ProviderResourceAssociation -} diff --git a/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/config.py b/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/config.py deleted file mode 100644 index 91af78576..000000000 --- a/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/config.py +++ /dev/null @@ -1,130 +0,0 @@ -# Copyright 2011 VMware, Inc., 2014 A10 Networks -# All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -""" -Routines for configuring Octavia Haproxy driver -""" - -from oslo_config import cfg - -from gbpservice._i18n import _ - - -haproxy_amphora_opts = [ - cfg.StrOpt('base_path', - default='/var/lib/octavia', - help=_('Base directory for amphora files.')), - cfg.StrOpt('base_cert_dir', - default='/var/lib/octavia/certs', - help=_('Base directory for cert storage.')), - cfg.StrOpt('haproxy_template', help=_('Custom haproxy template.')), - cfg.IntOpt('connection_max_retries', - default=10, - help=_('Retry threshold for connecting to amphorae.')), - cfg.IntOpt('connection_retry_interval', - default=5, - help=_('Retry timeout between connection attempts in ' - 'seconds.')), - cfg.StrOpt('haproxy_stick_size', default='10k', - help=_('Size of the HAProxy stick table. Accepts k, m, g ' - 'suffixes. Example: 10k')), - - # REST server - cfg.IPOpt('bind_host', default='0.0.0.0', - help=_("The host IP to bind to")), - cfg.PortOpt('bind_port', default=9443, - help=_("The port to bind to")), - cfg.StrOpt('haproxy_cmd', default='/usr/sbin/haproxy', - help=_("The full path to haproxy")), - cfg.IntOpt('respawn_count', default=2, - help=_("The respawn count for haproxy's upstart script")), - cfg.IntOpt('respawn_interval', default=2, - help=_("The respawn interval for haproxy's upstart script")), - cfg.FloatOpt('rest_request_conn_timeout', default=10, - help=_("The time in seconds to wait for a REST API " - "to connect.")), - cfg.FloatOpt('rest_request_read_timeout', default=60, - help=_("The time in seconds to wait for a REST API " - "response.")), - # REST client - cfg.StrOpt('client_cert', default='/etc/octavia/certs/client.pem', - help=_("The client certificate to talk to the agent")), - cfg.StrOpt('server_ca', default='/etc/octavia/certs/server_ca.pem', - help=_("The ca which signed the server certificates")), - cfg.BoolOpt('use_upstart', default=True, - help=_("If False, use sysvinit.")), -] - -certificate_opts = [ - cfg.StrOpt('cert_manager', - default='local_cert_manager', - help='Name of the cert manager to use'), - cfg.StrOpt('cert_generator', - default='local_cert_generator', - help='Name of the cert generator to use'), -] - -# Options only used by the amphora agent -amphora_agent_opts = [ - cfg.StrOpt('agent_server_ca', default='/etc/octavia/certs/client_ca.pem', - help=_("The ca which signed the client certificates")), - cfg.StrOpt('agent_server_cert', default='/etc/octavia/certs/server.pem', - help=_("The server certificate for the agent.py server " - "to use")), - cfg.StrOpt('agent_server_network_dir', - default='/etc/network/interfaces.d/', - help=_("The directory where new network interfaces " - "are located")), - cfg.StrOpt('agent_server_network_file', - help=_("The file where the network interfaces are located. " - "Specifying this will override any value set for " - "agent_server_network_dir.")), - # Do not specify in octavia.conf, loaded at runtime - cfg.StrOpt('amphora_id', help=_("The amphora ID.")), -] - -keepalived_vrrp_opts = [ - cfg.IntOpt('vrrp_advert_int', - default=1, - help=_('Amphora role and priority advertisement interval ' - 'in seconds.')), - cfg.IntOpt('vrrp_check_interval', - default=5, - help=_('VRRP health check script run interval in seconds.')), - cfg.IntOpt('vrrp_fail_count', - default=2, - help=_('Number of successive failure before transition to a ' - 'fail state.')), - cfg.IntOpt('vrrp_success_count', - default=2, - help=_('Number of successive failure before transition to a ' - 'success state.')), - cfg.IntOpt('vrrp_garp_refresh_interval', - default=5, - help=_('Time in seconds between gratuitous ARP announcements ' - 'from the MASTER.')), - cfg.IntOpt('vrrp_garp_refresh_count', - default=2, - help=_('Number of gratuitous ARP announcements to make on ' - 'each refresh interval.')) - -] - -# Register the configuration options -cfg.CONF.register_opts(amphora_agent_opts, group='amphora_agent') -cfg.CONF.register_opts(certificate_opts, group='certificates') -cfg.CONF.register_opts(haproxy_amphora_opts, group='haproxy_amphora') -cfg.CONF.register_opts(keepalived_vrrp_opts, group='keepalived_vrrp') -CONF = cfg.CONF diff --git a/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/haproxy_driver.py b/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/haproxy_driver.py deleted file mode 100644 index da7949c16..000000000 --- a/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/haproxy_driver.py +++ /dev/null @@ -1,699 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import ast -import copy - -from octavia.common import constants -from octavia.common import data_models as o_data_models -from octavia.network import data_models as network_data_models - -from gbpservice.contrib.nfp.configurator.drivers.base import base_driver -from gbpservice.contrib.nfp.configurator.drivers.loadbalancer.\ - v2.common import neutron_lbaas_data_models as n_data_models -from gbpservice.contrib.nfp.configurator.drivers.loadbalancer.\ - v2.haproxy import haproxy_driver_constants -from gbpservice.contrib.nfp.configurator.drivers.loadbalancer.\ - v2.haproxy.local_cert_manager import LocalCertManager -from gbpservice.contrib.nfp.configurator.drivers.loadbalancer.\ - v2.haproxy.rest_api_driver import HaproxyAmphoraLoadBalancerDriver -from gbpservice.contrib.nfp.configurator.lib import constants as common_const -from gbpservice.contrib.nfp.configurator.lib import data_parser -from gbpservice.contrib.nfp.configurator.lib import lbv2_constants -from gbpservice.nfp.common import exceptions -from gbpservice.nfp.core import log as nfp_logging - - -LOG = nfp_logging.getLogger(__name__) - - -# Copy from loadbalancer/v1/haproxy/haproxy_lb_driver.py -""" Loadbalancer generic configuration driver for handling device -configuration requests. -""" - - -class LbGenericConfigDriver(object): - """ - Driver class for implementing loadbalancer configuration - requests from Orchestrator. - """ - - def __init__(self): - pass - - def configure_interfaces(self, context, resource_data): - """ Configure interfaces for the service VM. - Calls static IP configuration function and implements - persistent rule addition in the service VM. - Issues REST call to service VM for configuration of interfaces. - :param context: neutron context - :param resource_data: a dictionary of loadbalancer objects - send by neutron plugin - Returns: SUCCESS/Failure message with reason. - """ - resource_data = self.parse.parse_data( - common_const.INTERFACES, resource_data) - mgmt_ip = resource_data['mgmt_ip'] - - try: - result_log_forward = self._configure_log_forwarding( - lbv2_constants.REQUEST_URL, mgmt_ip, - self.port) - except Exception as err: - msg = ("Failed to configure log forwarding for service at %s. " - "Error: %s" % (mgmt_ip, err)) - LOG.error(msg) - return msg - else: - if result_log_forward == common_const.UNHANDLED: - pass - elif result_log_forward != lbv2_constants.STATUS_SUCCESS: - msg = ("Failed to configure log forwarding for service at %s. " - % mgmt_ip) - LOG.error(msg) - return result_log_forward - else: - msg = ("Configured log forwarding for service at %s. " - "Result: %s" % (mgmt_ip, result_log_forward)) - LOG.info(msg) - - return lbv2_constants.STATUS_SUCCESS - - -# As we use the rest client and amphora image from Octavia, -# we need to have a helper class to simulate Octavia DB operation -# in order to get Octavia data models from Neutron-lbaas data models -class OctaviaDataModelBuilder(object): - - def __init__(self, driver=None): - self.driver = driver - - # All Octavia data models have these attributes - def _get_common_args(self, obj): - return { - 'id': obj.id, - 'project_id': obj.tenant_id, - 'name': obj.name, - 'description': obj.description, - 'enabled': obj.admin_state_up, - 'operating_status': obj.operating_status, - } - - # Update Octavia model from dict - def _update(self, octavia_data_model, update_dict): - for key, value in list(update_dict.items()): - setattr(octavia_data_model, key, value) - return octavia_data_model - - # Translate loadbalancer neutron model dict to octavia model - def get_loadbalancer_octavia_model(self, loadbalancer_dict): - loadbalancer = n_data_models.LoadBalancer.from_dict( - copy.deepcopy(loadbalancer_dict)) - ret = o_data_models.LoadBalancer() - args = self._get_common_args(loadbalancer) - vip = o_data_models.Vip( - load_balancer_id=loadbalancer.id, - ip_address=loadbalancer.vip_address, - subnet_id=loadbalancer.vip_subnet_id, - port_id=loadbalancer.vip_port.id, - load_balancer=ret - ) - amphorae = self.driver.get_amphora(loadbalancer.id) - if not amphorae: - raise exceptions.IncompleteData( - "Amphora information is missing") - # REVISIT(jiahao): cluster_group, topology, affinity_group_id are not - # included yet - args.update({ - 'vip': vip, - 'amphorae': amphorae, - 'provisioning_status': loadbalancer.provisioning_status, - }) - if loadbalancer_dict.get('listeners'): - listeners = [] - pools = [] - for listener_dict in loadbalancer_dict.get('listeners'): - listener = self.get_listener_octavia_model(listener_dict) - listener.load_balancer = ret - listeners.append(listener) - pools.extend(listener.pools) - for pool in listener.pools: - if pool.id not in [pool.id for pool in pools]: - pools.append(pool) - args.update({ - 'listeners': listeners, - 'pools': pools, - }) - - ret = self._update(ret, args) - return ret - - # Translate listener neutron model dict to octavia model - def get_listener_octavia_model(self, listener_dict): - # Must use a copy because from_dict will modify the original dict - listener = n_data_models.Listener.from_dict( - copy.deepcopy(listener_dict)) - ret = o_data_models.Listener() - args = self._get_common_args(listener) - sni_containers = [] - if listener_dict.get('sni_containers'): - for sni_dict in listener_dict.get('sni_containers'): - sni = o_data_models.SNI() - if sni_dict.get('listener'): - sni.listener = self.get_listener_octavia_model( - sni_dict.get('listener')) - sni.listener_id = sni_dict.get('listener_id') - sni.position = sni_dict.get('position') - sni.tls_container_id = sni_dict.get('tls_container_id') - sni_containers.append(sni) - if listener_dict.get('loadbalancer'): - loadbalancer = self.get_loadbalancer_octavia_model( - listener_dict.get('loadbalancer')) - if listener.id not in [_listener.id for _listener - in loadbalancer.listeners]: - loadbalancer.listeners.append(ret) - args.update({ - 'load_balancer': loadbalancer, - }) - if listener_dict.get('default_pool'): - pool = self.get_pool_octavia_model( - listener_dict.get('default_pool')) - if listener.id not in [_listener.id for _listener - in pool.listeners]: - pool.listeners.append(ret) - # REVISIT(jiahao): In Mitaka, we need to handle multiple pools - pools = [pool] - args.update({ - 'default_pool': pool, - 'pools': pools, - }) - args.update({ - 'load_balancer_id': listener.loadbalancer_id, - 'protocol': listener.protocol, - 'protocol_port': listener.protocol_port, - 'connection_limit': listener.connection_limit, - 'default_pool_id': listener.default_pool_id, - 'tls_certificate_id': listener.default_tls_container_id, - 'sni_containers': sni_containers, - 'provisioning_status': listener.provisioning_status, - }) - ret = self._update(ret, args) - return ret - - # Translate pool neutron model dict to octavia model - def get_pool_octavia_model(self, pool_dict): - pool = n_data_models.Pool.from_dict( - copy.deepcopy(pool_dict) - ) - ret = o_data_models.Pool() - args = self._get_common_args(pool) - # REVISIT(jiahao): In Mitaka, instead of pool.listener, - # there are pool.listeners. We need to handle that - if pool_dict.get('listener'): - listener = self.get_listener_octavia_model( - pool_dict.get('listener')) - if pool.id not in [_pool.id for _pool in listener.pools]: - listener.pools.append(ret) - if (not listener.default_pool) \ - or (listener.default_pool_id == pool.id): - listener.default_pool = ret - listeners = [listener] - args.update({ - 'listeners': listeners, - }) - if listener.load_balancer: - if pool.id not in [_pool.id for _pool - in listener.load_balancer.pools]: - listener.load_balancer.pools.append(ret) - args.update({ - 'load_balancer': listener.load_balancer, - 'load_balancer_id': listener.load_balancer_id, - }) - if pool_dict.get('members'): - members = [] - for member_dict in pool_dict.get('members'): - member = self.get_member_octavia_model(member_dict) - if not member.pool: - member.pool = ret - members.append(member) - args.update({ - 'members': members - }) - if pool_dict.get('healthmonitor'): - healthmonitor = self.get_healthmonitor_octavia_model( - pool_dict.get('healthmonitor')) - if not healthmonitor.pool: - healthmonitor.pool = ret - args.update({ - 'health_monitor': healthmonitor - }) - - # REVISIT(jiahao): L7Policy are not added - args.update({ - 'protocol': pool.protocol, - 'lb_algorithm': pool.lb_algorithm, - 'session_persistence': pool.session_persistence, - }) - ret = self._update(ret, args) - return ret - - # Translate member neutron model dict to octavia model - def get_member_octavia_model(self, member_dict): - member = n_data_models.Member.from_dict( - copy.deepcopy(member_dict) - ) - ret = o_data_models.Member() - args = { - 'id': member.id, - 'project_id': member.tenant_id, - 'pool_id': member.pool_id, - 'ip_address': member.address, - 'protocol_port': member.protocol_port, - 'weight': member.weight, - 'enabled': member.admin_state_up, - 'subnet_id': member.subnet_id, - 'operating_status': member.operating_status, - } - if member_dict.get('pool'): - pool = self.get_pool_octavia_model(member_dict.get('pool')) - args.update({ - 'pool': pool - }) - ret = self._update(ret, args) - return ret - - # Translate HealthMonitor neutron model dict to octavia model - def get_healthmonitor_octavia_model(self, hm_dict): - hm = n_data_models.HealthMonitor.from_dict( - copy.deepcopy(hm_dict) - ) - ret = o_data_models.HealthMonitor() - args = { - 'id': hm.id, - 'project_id': hm.tenant_id, - 'type': hm.type, - 'delay': hm.delay, - 'timeout': hm.timeout, - 'rise_threshold': hm.max_retries, - 'fall_threshold': hm.max_retries, - 'http_method': hm.http_method, - 'url_path': hm.url_path, - 'expected_codes': hm.expected_codes, - 'enabled': hm.admin_state_up - } - if hm_dict.get('pool'): - pool = self.get_pool_octavia_model(hm_dict.get('pool')) - args.update({ - 'pool': pool, - 'pool_id': pool.id - }) - ret = self._update(ret, args) - return ret - - -@base_driver.set_class_attr( - SERVICE_TYPE=lbv2_constants.SERVICE_TYPE, - SERVICE_VENDOR=haproxy_driver_constants.SERVICE_VENDOR) -class HaproxyLoadBalancerDriver(LbGenericConfigDriver, - base_driver.BaseDriver): - - # amphorae = {"loadbalancer_id": [o_data_models.Amphora( - # lb_network_ip, id, status)]} - amphorae = {} - - def __init__(self, plugin_rpc=None, conf=None): - # Each of the major LBaaS objects in the neutron database - # need a corresponding manager/handler class. - # - # Put common things that are shared across the entire driver, like - # config or a rest client handle, here. - # - # This function is executed when neutron-server starts. - super(HaproxyLoadBalancerDriver, self).__init__() - self.conf = conf - self.port = haproxy_driver_constants.CONFIGURATION_SERVER_PORT - self.parse = data_parser.DataParser() - self.amphora_driver = HaproxyAmphoraLoadBalancerDriver() - self.cert_manager = LocalCertManager() - - self.load_balancer = HaproxyLoadBalancerManager(self) - self.listener = HaproxyListenerManager(self) - self.pool = HaproxyPoolManager(self) - self.member = HaproxyMemberManager(self) - self.health_monitor = HaproxyHealthMonitorManager(self) - self.o_models_builder = OctaviaDataModelBuilder(self) - - @classmethod - def get_name(cls): - return haproxy_driver_constants.DRIVER_NAME - - # Get Amphora object given the loadbalancer_id - def get_amphora(self, loadbalancer_id): - return self.amphorae.get(loadbalancer_id) - - def add_amphora(self, context, loadbalancer_id, description, - status=constants.ACTIVE): - sc_metadata = ast.literal_eval(description) - rdata = self.parse.parse_data(common_const.LOADBALANCERV2, context) - if not (rdata['mgmt_ip'] and sc_metadata.get('network_function_id')): - raise exceptions.IncompleteData( - "Amphora information is missing") - if not self.get_amphora(loadbalancer_id): - # REVISIT(jiahao): use network_function_id as amphora id - amp = o_data_models.Amphora( - lb_network_ip=rdata['mgmt_ip'], - id=sc_metadata['network_function_id'], - status=status) - self.amphorae[loadbalancer_id] = [amp] - - -class HaproxyCommonManager(object): - - def __init__(self, driver): - self.driver = driver - self.parse = data_parser.DataParser() - - def _deploy(self, context, obj): - pass - - def create(self, context, obj): - msg = ("LB %s, created %s" % (self.__class__.__name__, obj['id'])) - LOG.info(msg) - - def update(self, context, old_obj, obj): - msg = ("LB %s, updated %s" % (self.__class__.__name__, obj['id'])) - LOG.info(msg) - - def delete(self, context, obj): - msg = ("LB %s, deleted %s" % (self.__class__.__name__, obj['id'])) - LOG.info(msg) - - def store_certs(self, listener_obj, listener_dict): - cert_mngr = self.driver.cert_manager - cert_ids = [] - if listener_obj.tls_certificate_id: - cert = listener_dict["default_tls_container"] - tls_certificate_id = cert_mngr.store_cert( - project_id=listener_dict["tenant_id"], - certificate=cert["certificate"], - private_key=cert["private_key"], - intermediates=cert["intermediates"] - ) - listener_obj.tls_certificate_id = tls_certificate_id - cert_ids.append(tls_certificate_id) - - if listener_obj.sni_containers: - for sni_cont in listener_obj.sni_containers: - for cont in listener_dict["sni_containers"]: - if sni_cont.tls_container_id == cont["tls_container_id"]: - cert = cont["tls_container"] - tls_certificate_id = cert_mngr.store_cert( - project_id=listener_dict["tenant_id"], - certificate=cert["certificate"], - private_key=cert["private_key"], - intermediates=cert["intermediates"] - ) - sni_cont.tls_container_id = tls_certificate_id - cert_ids.append(tls_certificate_id) - break - - return cert_ids - - def clean_certs(self, project_id, cert_ids): - cert_mngr = self.driver.cert_manager - for cert_id in cert_ids: - cert_mngr.delete_cert(project_id, cert_id) - - -class HaproxyLoadBalancerManager(HaproxyCommonManager): - - def _get_amphorae_network_config(self, - context, - loadbalancer_dict, - loadbalancer_o_obj): - loadbalancer_n_obj = n_data_models.LoadBalancer.from_dict( - copy.deepcopy(loadbalancer_dict)) - - amphorae_network_config = {} - - for amp in loadbalancer_o_obj.amphorae: - if amp.status != constants.DELETED: - # Get vip_subnet - vip_subnet = None - for subnet_dict in context['service_info']['subnets']: - if subnet_dict['id'] == loadbalancer_n_obj.vip_subnet_id: - vip_subnet = n_data_models.Subnet.from_dict( - copy.deepcopy(subnet_dict)) - break - if vip_subnet is None: - raise exceptions.IncompleteData( - "VIP subnet information is not found") - - sc_metadata = self.parse.parse_data( - common_const.LOADBALANCERV2, context) - vrrp_port = n_data_models.Port( - mac_address=sc_metadata['provider_mac']) - if vrrp_port is None: - raise exceptions.IncompleteData( - "VRRP port information is not found") - - amphorae_network_config[amp.id] = \ - network_data_models.AmphoraNetworkConfig( - amphora=amp, - vip_subnet=vip_subnet, - vrrp_port=vrrp_port) - - return amphorae_network_config - - def create(self, context, loadbalancer): - self.driver.add_amphora(context, loadbalancer['id'], - loadbalancer['description']) - loadbalancer_o_obj = self.driver.o_models_builder.\ - get_loadbalancer_octavia_model(loadbalancer) - amphorae_network_config = self._get_amphorae_network_config( - context, loadbalancer, loadbalancer_o_obj) - for amp in loadbalancer_o_obj.amphorae: - self.driver.amphora_driver.post_vip_plug( - amp, loadbalancer_o_obj, amphorae_network_config) - - msg = ("LB %s, created %s" - % (self.__class__.__name__, loadbalancer['id'])) - LOG.info(msg) - msg = ("Notified amphora of vip plug. " - "Loadbalancer id: %s, vip: %s" - % (loadbalancer['id'], loadbalancer_o_obj.vip.ip_address)) - LOG.info(msg) - - def update(self, context, old_loadbalancer, loadbalancer): - self.driver.add_amphora(context, loadbalancer['id'], - loadbalancer['description']) - loadbalancer_o_obj = self.driver.o_models_builder.\ - get_loadbalancer_octavia_model(loadbalancer) - for listener in loadbalancer_o_obj.listeners: - cert_ids = [] - for listener_dict in loadbalancer['listeners']: - if listener.id == listener_dict['id']: - cert_ids = self.store_certs(listener, listener_dict) - break - self.driver.amphora_driver.update(listener, loadbalancer_o_obj.vip) - self.clean_certs(loadbalancer['tenant_id'], cert_ids) - - msg = ("LB %s, updated %s" - % (self.__class__.__name__, loadbalancer['id'])) - LOG.info(msg) - - def delete(self, context, loadbalancer): - msg = ("LB %s, deleted %s" - % (self.__class__.__name__, loadbalancer['id'])) - LOG.info(msg) - # delete loadbalancer doesn't need any operation on service vm - - @property - def allocates_vip(self): - msg = ('allocates_vip queried') - LOG.info(msg) - return False - - def create_and_allocate_vip(self, context, loadbalancer): - msg = ("LB %s, create_and_allocate_vip %s" - % (self.__class__.__name__, loadbalancer['id'])) - LOG.info(msg) - self.create(context, loadbalancer) - - def refresh(self, context, loadbalancer): - # This is intended to trigger the backend to check and repair - # the state of this load balancer and all of its dependent objects - msg = ("LB pool refresh %s" % (loadbalancer['id'])) - LOG.info(msg) - - def stats(self, context, loadbalancer): - msg = ("LB stats %s" % (loadbalancer['id'])) - LOG.info(msg) - return { - "bytes_in": 0, - "bytes_out": 0, - "active_connections": 0, - "total_connections": 0 - } - - -class HaproxyListenerManager(HaproxyCommonManager): - - def _deploy(self, context, listener): - self.driver.add_amphora(context, listener['loadbalancer_id'], - listener['description']) - listener_o_obj = self.driver.o_models_builder.\ - get_listener_octavia_model(listener) - cert_ids = self.store_certs(listener_o_obj, listener) - self.driver.amphora_driver.update(listener_o_obj, - listener_o_obj.load_balancer.vip) - self.clean_certs(listener['tenant_id'], cert_ids) - - def create(self, context, listener): - self._deploy(context, listener) - msg = ("LB %s, created %s" % (self.__class__.__name__, listener['id'])) - LOG.info(msg) - - def update(self, context, old_listener, listener): - self._deploy(context, listener) - msg = ("LB %s, updated %s" % (self.__class__.__name__, listener['id'])) - LOG.info(msg) - - def delete(self, context, listener): - self.driver.add_amphora(context, listener['loadbalancer_id'], - listener['description']) - listener_o_obj = self.driver.o_models_builder.\ - get_listener_octavia_model(listener) - self.driver.amphora_driver.delete(listener_o_obj, - listener_o_obj.load_balancer.vip) - msg = ("LB %s, deleted %s" % (self.__class__.__name__, listener['id'])) - LOG.info(msg) - - -class HaproxyPoolManager(HaproxyCommonManager): - - def _remove_pool(self, pool): - pool_id = pool['id'] - # REVISIT(jiahao): In Mitaka, we need to handle multiple pools - default_pool = pool['listener']['default_pool'] - if default_pool['id'] == pool_id: - pool['listener']['default_pool'] = None - - def _deploy(self, context, pool): - self.driver.add_amphora(context, pool['loadbalancer_id'], - pool['description']) - pool_o_obj = self.driver.o_models_builder.\ - get_pool_octavia_model(pool) - # For Mitaka, that would be multiple listeners within pool - listener_o_obj = pool_o_obj.listeners[0] - load_balancer_o_obj = pool_o_obj.load_balancer - cert_ids = self.store_certs(listener_o_obj, - pool['listeners'][0]) - self.driver.amphora_driver.update(listener_o_obj, - load_balancer_o_obj.vip) - self.clean_certs(pool['tenant_id'], cert_ids) - - def create(self, context, pool): - self._deploy(context, pool) - msg = ("LB %s, created %s" % (self.__class__.__name__, pool['id'])) - LOG.info(msg) - - def update(self, context, old_pool, pool): - self._deploy(context, pool) - msg = ("LB %s, updated %s" % (self.__class__.__name__, pool['id'])) - LOG.info(msg) - - def delete(self, context, pool): - self._remove_pool(pool) - self._deploy(context, pool) - msg = ("LB %s, deleted %s" % (self.__class__.__name__, pool['id'])) - LOG.info(msg) - - -class HaproxyMemberManager(HaproxyCommonManager): - - def _deploy(self, context, member): - self.driver.add_amphora(context, member['pool']['loadbalancer_id'], - member['description']) - member_o_obj = self.driver.o_models_builder.\ - get_member_octavia_model(member) - listener_o_obj = member_o_obj.pool.listeners[0] - load_balancer_o_obj = member_o_obj.pool.load_balancer - cert_ids = self.store_certs(listener_o_obj, - member['pool']['listeners'][0]) - self.driver.amphora_driver.update(listener_o_obj, - load_balancer_o_obj.vip) - self.clean_certs(member['tenant_id'], cert_ids) - - def _remove_member(self, member): - member_id = member['id'] - # REVISIT(jiahao): In Mitaka, we need to handle multiple pools - default_pool = member['pool']['listener']['default_pool'] - for index, item in enumerate(default_pool['members']): - if item['id'] == member_id: - default_pool['members'].pop(index) - break - - def create(self, context, member): - self._deploy(context, member) - msg = ("LB %s, created %s" % (self.__class__.__name__, member['id'])) - LOG.info(msg) - - def update(self, context, old_member, member): - self._deploy(context, member) - msg = ("LB %s, updated %s" % (self.__class__.__name__, member['id'])) - LOG.info(msg) - - def delete(self, context, member): - self._remove_member(member) - self._deploy(context, member) - msg = ("LB %s, deleted %s" % (self.__class__.__name__, member['id'])) - LOG.info(msg) - - -class HaproxyHealthMonitorManager(HaproxyCommonManager): - - def _deploy(self, context, hm): - self.driver.add_amphora(context, hm['pool']['loadbalancer_id'], - hm['description']) - hm_o_obj = self.driver.o_models_builder.\ - get_healthmonitor_octavia_model(hm) - listener_o_obj = hm_o_obj.pool.listeners[0] - load_balancer_o_obj = hm_o_obj.pool.load_balancer - cert_ids = self.store_certs(listener_o_obj, - hm['pool']['listeners'][0]) - self.driver.amphora_driver.update(listener_o_obj, - load_balancer_o_obj.vip) - self.clean_certs(hm['tenant_id'], cert_ids) - - def _remove_healthmonitor(self, hm): - hm_id = hm['id'] - default_pool = hm['pool']['listener']['default_pool'] - if default_pool['healthmonitor']['id'] == hm_id: - default_pool['healthmonitor'] = None - - def create(self, context, hm): - self._deploy(context, hm) - msg = ("LB %s, created %s" % (self.__class__.__name__, hm['id'])) - LOG.info(msg) - - def update(self, context, old_hm, hm): - self._deploy(context, hm) - msg = ("LB %s, updated %s" % (self.__class__.__name__, hm['id'])) - LOG.info(msg) - - def delete(self, context, hm): - self._remove_healthmonitor(hm) - self._deploy(context, hm) - msg = ("LB %s, deleted %s" % (self.__class__.__name__, hm['id'])) - LOG.info(msg) diff --git a/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/haproxy_driver_constants.py b/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/haproxy_driver_constants.py deleted file mode 100644 index e436d018b..000000000 --- a/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/haproxy_driver_constants.py +++ /dev/null @@ -1,15 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -SERVICE_VENDOR = 'haproxy' -DRIVER_NAME = 'loadbalancerv2' -CONFIGURATION_SERVER_PORT = '9443' diff --git a/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/local_cert_manager.py b/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/local_cert_manager.py deleted file mode 100644 index 05f7525c8..000000000 --- a/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/local_cert_manager.py +++ /dev/null @@ -1,171 +0,0 @@ -# Copyright (c) 2014 Rackspace US, Inc -# All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import os -import uuid - -from octavia.certificates.common import local as local_common -from octavia.certificates.manager import cert_mgr -from octavia.common import exceptions -from oslo_config import cfg - -from gbpservice.nfp.core import log as nfp_logging - -LOG = nfp_logging.getLogger(__name__) - -CONF = cfg.CONF -CONF.import_group('certificates', 'octavia.certificates.common.local') -CONF.set_default('storage_path', '/tmp/', group='certificates') - - -class LocalCertManager(cert_mgr.CertManager): - """Cert Manager Interface that stores data locally.""" - - @staticmethod - def store_cert(project_id, certificate, private_key, intermediates=None, - private_key_passphrase=None, **kwargs): - """Stores (i.e., registers) a cert with the cert manager. - - This method stores the specified cert to the filesystem and returns - a UUID that can be used to retrieve it. - - :param project_id: Project ID for the owner of the certificate - :param certificate: PEM encoded TLS certificate - :param private_key: private key for the supplied certificate - :param intermediates: ordered and concatenated intermediate certs - :param private_key_passphrase: optional passphrase for the supplied key - - :returns: the UUID of the stored cert - :raises CertificateStorageException: if certificate storage fails - """ - cert_ref = str(uuid.uuid4()) - filename_base = os.path.join(CONF.certificates.storage_path, cert_ref) - - LOG.info( - "Storing certificate data on the local filesystem." - ) - try: - filename_certificate = "{0}.crt".format(filename_base, cert_ref) - with open(filename_certificate, 'w') as cert_file: - cert_file.write(certificate) - - filename_private_key = "{0}.key".format(filename_base, cert_ref) - with open(filename_private_key, 'w') as key_file: - key_file.write(private_key) - - if intermediates: - filename_intermediates = "{0}.int".format(filename_base, - cert_ref) - with open(filename_intermediates, 'w') as int_file: - int_file.write(intermediates) - - if private_key_passphrase: - filename_pkp = "{0}.pass".format(filename_base, cert_ref) - with open(filename_pkp, 'w') as pass_file: - pass_file.write(private_key_passphrase) - except IOError as ioe: - LOG.error("Failed to store certificate.") - raise exceptions.CertificateStorageException(message=ioe.message) - - return cert_ref - - @staticmethod - def get_cert(project_id, cert_ref, **kwargs): - """Retrieves the specified cert. - - :param project_id: Project ID for the owner of the certificate - :param cert_ref: the UUID of the cert to retrieve - - :return: octavia.certificates.common.Cert representation of the - certificate data - :raises CertificateStorageException: if certificate retrieval fails - """ - LOG.info( - "Loading certificate {0} from the local filesystem.".format( - cert_ref)) - - filename_base = os.path.join(CONF.certificates.storage_path, cert_ref) - - filename_certificate = "{0}.crt".format(filename_base, cert_ref) - filename_private_key = "{0}.key".format(filename_base, cert_ref) - filename_intermediates = "{0}.int".format(filename_base, cert_ref) - filename_pkp = "{0}.pass".format(filename_base, cert_ref) - - cert_data = dict() - - try: - with open(filename_certificate, 'r') as cert_file: - cert_data['certificate'] = cert_file.read() - except IOError: - LOG.error( - "Failed to read certificate for {0}.".format(cert_ref)) - raise exceptions.CertificateStorageException( - msg="Certificate could not be read." - ) - try: - with open(filename_private_key, 'r') as key_file: - cert_data['private_key'] = key_file.read() - except IOError: - LOG.error( - "Failed to read private key for {0}.".format(cert_ref)) - raise exceptions.CertificateStorageException( - msg="Private Key could not be read." - ) - - try: - with open(filename_intermediates, 'r') as int_file: - cert_data['intermediates'] = int_file.read() - except IOError: - pass - - try: - with open(filename_pkp, 'r') as pass_file: - cert_data['private_key_passphrase'] = pass_file.read() - except IOError: - pass - - return local_common.LocalCert(**cert_data) - - @staticmethod - def delete_cert(project_id, cert_ref, **kwargs): - """Deletes the specified cert. - - :param project_id: Project ID for the owner of the certificate - :param cert_ref: the UUID of the cert to delete - - :raises CertificateStorageException: if certificate deletion fails - """ - LOG.info( - "Deleting certificate {0} from the local filesystem.".format( - cert_ref)) - - filename_base = os.path.join(CONF.certificates.storage_path, cert_ref) - - filename_certificate = "{0}.crt".format(filename_base, cert_ref) - filename_private_key = "{0}.key".format(filename_base, cert_ref) - filename_intermediates = "{0}.int".format(filename_base, cert_ref) - filename_pkp = "{0}.pass".format(filename_base, cert_ref) - - try: - os.remove(filename_certificate) - os.remove(filename_private_key) - if os.path.exists(filename_intermediates): - os.remove(filename_intermediates) - if os.path.exists(filename_pkp): - os.remove(filename_pkp) - except IOError as ioe: - LOG.error( - "Failed to delete certificate {0}.".format(cert_ref)) - raise exceptions.CertificateStorageException(message=ioe.message) diff --git a/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/rest_api_driver.py b/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/rest_api_driver.py deleted file mode 100644 index 26bd1e44f..000000000 --- a/gbpservice/contrib/nfp/configurator/drivers/loadbalancer/v2/haproxy/rest_api_driver.py +++ /dev/null @@ -1,103 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import sys -import time -import warnings - -# Override unnecessary Octavia config import -from gbpservice.contrib.nfp.configurator.drivers.loadbalancer.v2.haproxy \ - import config -from gbpservice.contrib.nfp.configurator.drivers.loadbalancer.v2.haproxy.\ - config import cfg -sys.modules['octavia.common.config'] = config -sys.modules['octavia.common.config.cfg'] = cfg - -from octavia.amphorae.driver_exceptions import exceptions as driver_except -from octavia.amphorae.drivers.haproxy import rest_api_driver -from octavia.common.jinja.haproxy import jinja_cfg -from oslo_config import cfg -import requests - -from gbpservice.contrib.nfp.configurator.drivers.loadbalancer.v2.haproxy.\ - local_cert_manager import LocalCertManager -from gbpservice.nfp.core import log as nfp_logging - -LOG = nfp_logging.getLogger(__name__) -API_VERSION = rest_api_driver.API_VERSION -OCTAVIA_API_CLIENT = rest_api_driver.OCTAVIA_API_CLIENT - -CONF = cfg.CONF -CONF.import_group('haproxy_amphora', 'octavia.common.config') - - -class HaproxyAmphoraLoadBalancerDriver( - rest_api_driver.HaproxyAmphoraLoadBalancerDriver): - - def __init__(self): - super(rest_api_driver.HaproxyAmphoraLoadBalancerDriver, - self).__init__() - self.client = AmphoraAPIClient() - self.cert_manager = LocalCertManager() - self.jinja = jinja_cfg.JinjaTemplater( - base_amp_path=CONF.haproxy_amphora.base_path, - base_crt_dir=CONF.haproxy_amphora.base_cert_dir, - haproxy_template=CONF.haproxy_amphora.haproxy_template) - - -class AmphoraAPIClient(rest_api_driver.AmphoraAPIClient): - """Removed SSL verification from original api client""" - - def __init__(self): - super(AmphoraAPIClient, self).__init__() - self.session = requests.Session() - - def _base_url(self, ip): - return "http://{ip}:{port}/{version}/".format( - ip=ip, - port=CONF.haproxy_amphora.bind_port, - version=API_VERSION) - - def request(self, method, amp, path='/', **kwargs): - LOG.debug("request url %s", path) - _request = getattr(self.session, method.lower()) - _url = self._base_url(amp.lb_network_ip) + path - LOG.debug("request url " + _url) - timeout_tuple = (CONF.haproxy_amphora.rest_request_conn_timeout, - CONF.haproxy_amphora.rest_request_read_timeout) - reqargs = { - 'url': _url, - 'timeout': timeout_tuple, } - reqargs.update(kwargs) - headers = reqargs.setdefault('headers', {}) - - headers['User-Agent'] = OCTAVIA_API_CLIENT - # Keep retrying - for a in range(CONF.haproxy_amphora.connection_max_retries): - try: - with warnings.catch_warnings(): - warnings.filterwarnings( - "ignore", - message="A true SSLContext object is not available" - ) - r = _request(**reqargs) - LOG.debug("Connected to amphora. Response: {resp}".format( - resp=r)) - return r - except (requests.ConnectionError, requests.Timeout): - LOG.warning("Could not connect to instance. Retrying.") - time.sleep(CONF.haproxy_amphora.connection_retry_interval) - - LOG.error("Connection retries (currently set to %s) " - "exhausted. The amphora is unavailable.", - CONF.haproxy_amphora.connection_max_retries) - raise driver_except.TimeOutException() diff --git a/gbpservice/contrib/nfp/configurator/drivers/nfp_service/__init__.py b/gbpservice/contrib/nfp/configurator/drivers/nfp_service/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/configurator/drivers/nfp_service/heat/__init__.py b/gbpservice/contrib/nfp/configurator/drivers/nfp_service/heat/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/configurator/drivers/nfp_service/heat/heat_driver.py b/gbpservice/contrib/nfp/configurator/drivers/nfp_service/heat/heat_driver.py deleted file mode 100644 index 75dab6e6e..000000000 --- a/gbpservice/contrib/nfp/configurator/drivers/nfp_service/heat/heat_driver.py +++ /dev/null @@ -1,42 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from gbpservice.nfp.core import log as nfp_logging - - -from gbpservice.contrib.nfp.configurator.drivers.base import base_driver -from gbpservice.contrib.nfp.configurator.lib import ( - nfp_service_constants as const) - -LOG = nfp_logging.getLogger(__name__) - - -class HeatDriver(base_driver.BaseDriver): - """ Heat as a driver for handling config script - heat configuration requests. - - We initialize service type in this class because agent loads - class object only for those driver classes that have service type - initialized. Also, only this driver class is exposed to the agent. - - """ - - service_type = const.SERVICE_TYPE - resource_type = const.HEAT_RESOURCE - - def __init__(self, conf): - pass - - def run_heat(self, context, kwargs): - msg = ("Heat template execution request received but unhandled") - LOG.info(msg) - return const.UNHANDLED_RESULT diff --git a/gbpservice/contrib/nfp/configurator/drivers/vpn/__init__.py b/gbpservice/contrib/nfp/configurator/drivers/vpn/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/configurator/drivers/vpn/vyos/__init__.py b/gbpservice/contrib/nfp/configurator/drivers/vpn/vyos/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/configurator/drivers/vpn/vyos/vyos_vpn_constants.py b/gbpservice/contrib/nfp/configurator/drivers/vpn/vyos/vyos_vpn_constants.py deleted file mode 100644 index 738341d72..000000000 --- a/gbpservice/contrib/nfp/configurator/drivers/vpn/vyos/vyos_vpn_constants.py +++ /dev/null @@ -1,18 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - - -SERVICE_VENDOR = 'vyos' -CONFIGURATION_SERVER_PORT = 8888 -request_url = "http://%s:%s/%s" - -REST_TIMEOUT = 180 diff --git a/gbpservice/contrib/nfp/configurator/drivers/vpn/vyos/vyos_vpn_driver.py b/gbpservice/contrib/nfp/configurator/drivers/vpn/vyos/vyos_vpn_driver.py deleted file mode 100644 index 4d9ee1675..000000000 --- a/gbpservice/contrib/nfp/configurator/drivers/vpn/vyos/vyos_vpn_driver.py +++ /dev/null @@ -1,1429 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - - -import copy -import time - -from oslo_concurrency import lockutils -from oslo_serialization import jsonutils -import requests -import six - - -from gbpservice.contrib.nfp.configurator.drivers.base import base_driver -from gbpservice.contrib.nfp.configurator.drivers.vpn.vyos import ( - vyos_vpn_constants as const) -from gbpservice.contrib.nfp.configurator.lib import ( - generic_config_constants as gen_cfg_const) -from gbpservice.contrib.nfp.configurator.lib import constants as common_const -from gbpservice.contrib.nfp.configurator.lib import data_parser -from gbpservice.contrib.nfp.configurator.lib import vpn_constants as vpn_const -from gbpservice.nfp.core import log as nfp_logging - -LOG = nfp_logging.getLogger(__name__) - - -class UnknownReasonException(Exception): - message = "Unsupported rpcreason '%(reason)s' from plugin " - - -class UnknownResourceException(Exception): - message = "Unsupported resource '%(resource)s' from plugin " - - -class RestApi(object): - """ - Provides different methods to make ReST calls to the service VM, - to update the configurations - """ - - def __init__(self, vm_mgmt_ip): - self.vm_mgmt_ip = vm_mgmt_ip - self.timeout = const.REST_TIMEOUT - - def _dict_to_query_str(self, args): - return '&'.join([str(k) + '=' + str(v) - for k, v in six.iteritems(args)]) - - def post(self, api, args, headers): - """ - Makes ReST call to the service VM to post the configurations. - - :param api: method that need to called inside the service VM to - update the configurations. - :prarm args: data that is need to be configured in service VM - - Returns: None - """ - url = const.request_url % ( - self.vm_mgmt_ip, - const.CONFIGURATION_SERVER_PORT, api) - data = jsonutils.dumps(args) - - try: - resp = requests.post(url, data=data, timeout=self.timeout, - headers=headers) - message = jsonutils.loads(resp.text) - msg = "POST url %s %d" % (url, resp.status_code) - LOG.debug(msg) - if resp.status_code == 200 and message.get("status", False): - msg = "POST Rest API %s - Success" % (url) - LOG.debug(msg) - else: - msg = ("POST Rest API %s - Failed with status %s, %s" - % (url, resp.status_code, - message.get("reason", None))) - LOG.error(msg) - raise requests.exceptions.HTTPError(msg) - except Exception as err: - msg = ("Post Rest API %s - Failed. Reason: %s" - % (url, str(err).capitalize())) - LOG.error(msg) - raise requests.exceptions.HTTPError(msg) - - def put(self, api, args, headers): - """ - Makes ReST call to the service VM to put the configurations. - - :param api: method that need to called inside the service VM to - update the configurations. - :prarm args: data that is need to be configured in service VM - - Returns: None - """ - url = const.request_url % ( - self.vm_mgmt_ip, - const.CONFIGURATION_SERVER_PORT, api) - data = jsonutils.dumps(args) - - try: - resp = requests.put(url, data=data, timeout=self.timeout, - headers=headers) - msg = "PUT url %s %d" % (url, resp.status_code) - LOG.debug(msg) - if resp.status_code == 200: - msg = "REST API PUT %s succeeded." % url - LOG.debug(msg) - else: - msg = ("REST API PUT %s failed with status: %d." - % (url, resp.status_code)) - LOG.error(msg) - except Exception as err: - msg = ("REST API for PUT %s failed. %s" - % (url, str(err).capitalize())) - LOG.error(msg) - - def delete(self, api, args, headers, data=None): - """ - Makes ReST call to the service VM to delete the configurations. - - :param api: method that need to called inside the service VM to - update the configurations. - :param args: fixed ip of the service VM to make frame the query string. - :data args: data that is need to be configured in service VM - - Returns: None - """ - url = const.request_url % ( - self.vm_mgmt_ip, - const.CONFIGURATION_SERVER_PORT, api) - - if args: - url += '?' + self._dict_to_query_str(args) - - if data: - data = jsonutils.dumps(data) - try: - resp = requests.delete(url, timeout=self.timeout, data=data, - headers=headers) - message = jsonutils.loads(resp.text) - msg = "DELETE url %s %d" % (url, resp.status_code) - LOG.debug(msg) - if resp.status_code == 200 and message.get("status", False): - msg = "DELETE Rest API %s - Success" % (url) - LOG.info(msg) - else: - msg = ("DELETE Rest API %s - Failed %s" - % (url, message.get("reason", None))) - LOG.error(msg) - raise requests.exceptions.HTTPError(msg) - except Exception as err: - msg = ("Delete Rest API %s - Failed. Reason: %s" - % (url, str(err).capitalize())) - LOG.error(msg) - raise requests.exceptions.HTTPError(msg) - - def get(self, api, args, headers): - """ - Makes ReST call to the service VM to put the configurations. - - :param api: method that need to called inside the service VM to - update the configurations. - :prarm args: data that is need to be configured in service VM - - Returns: None - """ - output = '' - - url = const.request_url % ( - self.vm_mgmt_ip, - const.CONFIGURATION_SERVER_PORT, api) - - try: - resp = requests.get(url, params=args, timeout=self.timeout, - headers=headers) - msg = "GET url %s %d" % (url, resp.status_code) - LOG.debug(msg) - if resp.status_code == 200: - msg = "REST API GET %s succeeded." % url - LOG.debug(msg) - json_resp = resp.json() - return json_resp - else: - msg = ("REST API GET %s failed with status: %d." - % (url, resp.status_code)) - LOG.error(msg) - except requests.exceptions.Timeout as err: - msg = ("REST API GET %s timed out. %s." - % (url, str(err).capitalize())) - LOG.error(msg) - except Exception as err: - msg = ("REST API for GET %s failed. %s" - % (url, str(err).capitalize())) - LOG.error(msg) - - return output - - -class VPNServiceValidator(object): - """ - Provides the methods to validate the vpn service which is about to - be created in order to avoid any conflicts if they exists. - """ - - def __init__(self, agent): - self.agent = agent - - def _update_service_status(self, vpnsvc, status): - """ - Driver will call this API to report - status of VPN service. - """ - msg = ("Driver informing status: %s." - % status) - LOG.debug(msg) - vpnsvc_status = [{ - 'id': vpnsvc['id'], - 'status': status, - 'updated_pending_status':True}] - return vpnsvc_status - - def _error_state(self, context, vpnsvc, message=''): - """ - Enqueues the status of the service to ERROR. - - :param context: Dictionary which holds all the required data for - for vpn service. - :param vpnsvc: vpn service dictionary. - :param message: the cause for the error. - - Returns: None - """ - self.agent.update_status( - context, self._update_service_status(vpnsvc, - vpn_const.STATE_ERROR)) - msg = ("Resource vpn service: %r went " - "to error state, %r" % (vpnsvc['id'], message)) - raise Exception(msg) - - def _active_state(self, context, vpnsvc): - """ - Enqueues the status of the service to ACTIVE. - - :param context: Dictionary which holds all the required data for - for vpn service. - :param vpnsvc: vpn service dictionary. - - Returns: None - """ - self.agent.update_status( - context, self._update_service_status(vpnsvc, - vpn_const.STATE_ACTIVE)) - - def _get_local_cidr(self, vpn_svc): - # REVISIT: position based parsing of description - svc_desc = vpn_svc['description'] - tokens = svc_desc.split(';') - local_cidr = tokens[1].split('=')[1] - return local_cidr - - def validate(self, context, resource_data): - """ - Get the vpn services for this tenant - Check for overlapping lcidr - (not allowed) - - :param context: Dictionary which holds all the required data for - for vpn service. - :param vpnsvc: vpn service dictionary. - - Returns: None - """ - - vpnsvc = resource_data.get('resource') - lcidr = resource_data.get('provider_cidr') - filters = {'tenant_id': [context['tenant_id']]} - t_vpnsvcs = self.agent.get_vpn_services( - context, filters=filters) - vpnsvc.pop("status", None) - - for svc in t_vpnsvcs: - del svc['status'] - if vpnsvc in t_vpnsvcs: - t_vpnsvcs.remove(vpnsvc) - for svc in t_vpnsvcs: - t_lcidr = self._get_local_cidr(svc) - if t_lcidr == lcidr: - msg = ("Local cidr %s conflicts with existing vpn service %s" - % (lcidr, svc['id'])) - LOG.error(msg) - self._error_state( - context, - vpnsvc, msg) - self._active_state(context, vpnsvc) - - -class VpnGenericConfigDriver(base_driver.BaseDriver): - """ - VPN generic config driver for handling device configurations requests. - This driver class implements VPN configuration. - """ - - def __init__(self): - self.timeout = const.REST_TIMEOUT - self.parse = data_parser.DataParser() - - def _parse_vm_context(self, context): - try: - username = str(context['service_vm_context'][ - 'vyos']['username']) - password = str(context['service_vm_context'][ - 'vyos']['password']) - headers = {'Content-Type': 'application/json', - 'username': username, - 'password': password} - return headers - except Exception as e: - msg = ("Failed to get header from context. ERROR: %s" % e) - LOG.error(msg) - raise Exception(msg) - - def configure_healthmonitor(self, context, resource_data): - vm_status = super(VpnGenericConfigDriver, - self).configure_healthmonitor( - context, resource_data) - if resource_data['nfds'][0]['periodicity'] == gen_cfg_const.INITIAL: - if vm_status == common_const.SUCCESS: - try: - resp = self.configure_user(context, resource_data) - if resp not in common_const.SUCCESS_CODES: - return common_const.FAILURE - except Exception as e: - msg = ("Failed to configure user. ERROR: %s" % e) - LOG.error(msg) - return common_const.FAILURE - return vm_status - - def configure_user(self, context, resource_data): - headers = self._parse_vm_context(context) - resource_data = self.parse.parse_data(common_const.HEALTHMONITOR, - resource_data) - mgmt_ip = resource_data['mgmt_ip'] - url = const.request_url % (mgmt_ip, - self.port, - 'change_auth') - data = {} - - LOG.info("Initiating POST request to configure Authentication " - "service at mgmt ip:%(mgmt_ip)s", - {'mgmt_ip': mgmt_ip}) - err_msg = ("Change Auth POST request to the VyOS firewall " - "service at %s failed. " % url) - try: - resp = requests.post(url, data=data, headers=headers) - except Exception as err: - err_msg += ("Reason: %r" % str(err).capitalize()) - LOG.error(err_msg) - return err_msg - - if (resp.status_code in common_const.SUCCESS_CODES) and ( - resp.json().get('status') is True): - msg = ("Configured user authentication successfully" - " for vyos service at %r." % mgmt_ip) - LOG.info(msg) - return resp.status_code - - err_msg += (("Failed to change Authentication para Status code" - ": %r, Reason: %r" % - (resp.status_code, resp.json().get('reason'))) - if type(resp.json()) is dict - else ("Reason: " + resp)) - LOG.error(err_msg) - return err_msg - - def _configure_static_ips(self, context, resource_data): - """ Configure static IPs for provider and stitching interfaces - of service VM. - - Issues REST call to service VM for configuration of static IPs. - - :param resource_data: a dictionary of vpn rules and objects - send by neutron plugin - - Returns: SUCCESS/Failure message with reason. - - """ - headers = self._parse_vm_context(context) - static_ips_info = dict( - provider_ip=resource_data.get('provider_ip'), - provider_cidr=resource_data.get('provider_cidr'), - provider_mac=resource_data.get('provider_mac'), - stitching_ip=resource_data.get('stitching_ip'), - stitching_cidr=resource_data.get('stitching_cidr'), - stitching_mac=resource_data.get('stitching_mac')) - mgmt_ip = resource_data['mgmt_ip'] - - url = const.request_url % (mgmt_ip, - const.CONFIGURATION_SERVER_PORT, - 'add_static_ip') - data = jsonutils.dumps(static_ips_info) - - msg = ("Initiating POST request to add static IPs for primary " - "service at: %r" % mgmt_ip) - LOG.info(msg) - try: - resp = requests.post(url, data, timeout=self.timeout, - headers=headers) - except requests.exceptions.ConnectionError as err: - msg = ("Failed to establish connection to primary service at: " - "%r. ERROR: %r" % - (mgmt_ip, str(err).capitalize())) - LOG.error(msg) - return msg - except requests.exceptions.RequestException as err: - msg = ("Unexpected ERROR happened while adding " - "static IPs for primary service at: %r. " - "ERROR: %r" % - (mgmt_ip, str(err).capitalize())) - LOG.error(msg) - return msg - - try: - result = resp.json() - except ValueError as err: - msg = ("Unable to parse response, invalid JSON. URL: " - "%r. %r" % (url, str(err).capitalize())) - LOG.error(msg) - return msg - if not result['status']: - msg = ("Error adding static IPs. URL: %r. Reason: %s." % - (url, result['reason'])) - LOG.error(msg) - return msg - - msg = ("Static IPs successfully added.") - LOG.info(msg) - return common_const.STATUS_SUCCESS - - def configure_interfaces(self, context, resource_data): - """ Configure interfaces for the service VM. - - Calls static IP configuration function and implements - persistent rule addition in the service VM. - Issues REST call to service VM for configuration of interfaces. - - :param context: neutron context - :param resource_data: a dictionary of vpn rules and objects - send by neutron plugin - - Returns: SUCCESS/Failure message with reason. - - """ - headers = self._parse_vm_context(context) - resource_data = self.parse.parse_data(common_const.INTERFACES, - resource_data) - mgmt_ip = resource_data['mgmt_ip'] - - try: - result_log_forward = self._configure_log_forwarding( - const.request_url, mgmt_ip, self.port, headers) - except Exception as err: - msg = ("Failed to configure log forwarding for service at %s. " - "Error: %s" % (mgmt_ip, err)) - LOG.error(msg) - else: - if result_log_forward == common_const.UNHANDLED: - pass - elif result_log_forward != common_const.STATUS_SUCCESS: - # Failure in log forward configuration won't break chain - # creation. However, error will be logged for detecting - # failure. - msg = ("Failed to configure log forwarding for service at %s." - " Error: %s" % (mgmt_ip, result_log_forward)) - LOG.error(msg) - - try: - result_static_ips = self._configure_static_ips(context, - resource_data) - except Exception as err: - msg = ("Failed to add static IPs. Error: %s" % err) - LOG.error(msg) - return msg - else: - if result_static_ips != common_const.STATUS_SUCCESS: - return result_static_ips - else: - msg = ("Added static IPs. Result: %s" % result_static_ips) - LOG.info(msg) - - rule_info = dict( - provider_mac=resource_data['provider_mac'], - stitching_mac=resource_data['stitching_mac']) - - url = const.request_url % (mgmt_ip, - const.CONFIGURATION_SERVER_PORT, 'add_rule') - data = jsonutils.dumps(rule_info) - msg = ("Initiating POST request to add persistent rule to primary " - "service at: %r" % mgmt_ip) - LOG.info(msg) - try: - resp = requests.post(url, data, timeout=self.timeout, - headers=headers) - except requests.exceptions.ConnectionError as err: - msg = ("Failed to establish connection to primary service at: " - "%r. ERROR: %r" % - (mgmt_ip, str(err).capitalize())) - LOG.error(msg) - return msg - except requests.exceptions.RequestException as err: - msg = ("Unexpected ERROR happened while adding " - "persistent rule of primary service at: %r. ERROR: %r" % - (mgmt_ip, str(err).capitalize())) - LOG.error(msg) - return msg - - try: - result = resp.json() - except ValueError as err: - msg = ("Unable to parse response, invalid JSON. URL: " - "%r. %r" % (url, str(err).capitalize())) - LOG.error(msg) - return msg - if not result['status']: - msg = ("Error adding persistent rule. URL: %r" % url) - LOG.error(msg) - return msg - - msg = ("Persistent rule successfully added.") - LOG.info(msg) - # wait for 10secs for the ip address to get configured. Sometimes - # observed that 'set_routes' fail with 'ip not configured' error. - time.sleep(10) - return common_const.STATUS_SUCCESS - - def _clear_static_ips(self, context, resource_data): - """ Clear static IPs for provider and stitching - interfaces of the service VM. - - Issues REST call to service VM for deletion of static IPs. - - :param resource_data: a dictionary of vpn rules and objects - send by neutron plugin - - Returns: SUCCESS/Failure message with reason. - - """ - headers = self._parse_vm_context(context) - static_ips_info = dict( - provider_ip=resource_data.get('provider_ip'), - provider_cidr=resource_data.get('provider_cidr'), - provider_mac=resource_data.get('provider_mac'), - stitching_ip=resource_data.get('stitching_ip'), - stitching_cidr=resource_data.get('stitching_cidr'), - stitching_mac=resource_data.get('stitching_mac')) - mgmt_ip = resource_data['mgmt_ip'] - - url = const.request_url % (mgmt_ip, - const.CONFIGURATION_SERVER_PORT, - 'del_static_ip') - data = jsonutils.dumps(static_ips_info) - - msg = ("Initiating POST request to remove static IPs for primary " - "service at: %r" % mgmt_ip) - LOG.info(msg) - try: - resp = requests.delete(url, data=data, timeout=self.timeout, - headers=headers) - except requests.exceptions.ConnectionError as err: - msg = ("Failed to establish connection to primary service at: " - "%r. ERROR: %r" % - (mgmt_ip, str(err).capitalize())) - LOG.error(msg) - return msg - except requests.exceptions.RequestException as err: - msg = ("Unexpected ERROR happened while removing " - "static IPs for primary service at: %r. ERROR: %r" % - (mgmt_ip, str(err).capitalize())) - LOG.error(msg) - return msg - - try: - result = resp.json() - except ValueError as err: - msg = ("Unable to parse response, invalid JSON. URL: " - "%r. %r" % (url, str(err).capitalize())) - LOG.error(msg) - return msg - if not result['status']: - msg = ("Error removing static IPs. URL: %r. Reason: %s." % - (url, result['reason'])) - LOG.error(msg) - return msg - - msg = ("Static IPs successfully removed.") - LOG.info(msg) - return common_const.STATUS_SUCCESS - - def clear_interfaces(self, context, resource_data): - """ Clear interfaces for the service VM. - - Calls static IP clear function and implements - persistent rule deletion in the service VM. - Issues REST call to service VM for deletion of interfaces. - - :param context: neutron context - :param resource_data: a dictionary of vpn rules and objects - send by neutron plugin - - Returns: SUCCESS/Failure message with reason. - - """ - headers = self._parse_vm_context(context) - resource_data = self.parse.parse_data(common_const.INTERFACES, - resource_data) - try: - result_static_ips = self._clear_static_ips(context, resource_data) - except Exception as err: - msg = ("Failed to remove static IPs. Error: %s" % err) - LOG.error(msg) - return msg - else: - if result_static_ips != common_const.STATUS_SUCCESS: - return result_static_ips - else: - msg = ("Successfully removed static IPs. " - "Result: %s" % result_static_ips) - LOG.info(msg) - - rule_info = dict( - provider_mac=resource_data['provider_mac'], - stitching_mac=resource_data['stitching_mac']) - - mgmt_ip = resource_data['mgmt_ip'] - - msg = ("Initiating DELETE persistent rule.") - LOG.info(msg) - url = const.request_url % (mgmt_ip, - const.CONFIGURATION_SERVER_PORT, - 'delete_rule') - - try: - data = jsonutils.dumps(rule_info) - resp = requests.delete(url, data=data, timeout=self.timeout, - headers=headers) - except requests.exceptions.ConnectionError as err: - msg = ("Failed to establish connection to service at: %r. " - "ERROR: %r" % - (mgmt_ip, str(err).capitalize())) - LOG.error(msg) - raise Exception(err) - except requests.exceptions.RequestException as err: - msg = ("Unexpected ERROR happened while deleting " - "persistent rule of service at: %r. ERROR: %r" % - (mgmt_ip, str(err).capitalize())) - LOG.error(msg) - raise Exception(err) - - try: - result = resp.json() - except ValueError as err: - msg = ("Unable to parse response, invalid JSON. URL: " - "%r. %r" % (url, str(err).capitalize())) - LOG.error(msg) - raise Exception(msg) - if not result['status'] or resp.status_code not in [200, 201, 202]: - msg = ("Error deleting persistent rule. URL: %r" % url) - LOG.error(msg) - raise Exception(msg) - msg = ("Persistent rule successfully deleted.") - LOG.info(msg) - return common_const.STATUS_SUCCESS - - def configure_routes(self, context, resource_data): - """ Configure routes for the service VM. - - Issues REST call to service VM for configuration of routes. - - :param context: neutron context - :param resource_data: a dictionary of vpn rules and objects - send by neutron plugin - - Returns: SUCCESS/Failure message with reason. - - """ - headers = self._parse_vm_context(context) - forward_routes = resource_data.get('forward_route') - resource_data = self.parse.parse_data(common_const.ROUTES, - resource_data) - mgmt_ip = resource_data.get('mgmt_ip') - gateway_ip = resource_data.get('stitching_gw_ip') - - # checking whether VPN service is present in the chain - # if yes, just configure the stitching pbr else - # configure both stitching and provider pbrs. - - if not forward_routes: - source_cidrs = [resource_data.get('stitching_cidr')] - else: - source_cidrs = [resource_data.get('provider_cidr'), - resource_data.get('stitching_cidr')] - - stitching_url = const.request_url % (mgmt_ip, - const.CONFIGURATION_SERVER_PORT, - 'add-stitching-route') - st_data = jsonutils.dumps({'gateway_ip': gateway_ip}) - - try: - resp = requests.post( - stitching_url, data=st_data, timeout=self.timeout, - headers=headers) - except requests.exceptions.ConnectionError as err: - msg = ("Failed to establish connection to service at: " - "%r. ERROR: %r" % (mgmt_ip, - str(err).capitalize())) - LOG.error(msg) - return msg - except requests.exceptions.RequestException as err: - msg = ("Unexpected ERROR happened while configuring " - "default gw route of service at: %r ERROR: %r" % - (mgmt_ip, str(err).capitalize())) - LOG.error(msg) - return msg - - url = const.request_url % (mgmt_ip, const.CONFIGURATION_SERVER_PORT, - 'add-source-route') - active_configured = False - route_info = [] - for source_cidr in source_cidrs: - route_info.append({'source_cidr': source_cidr, - 'gateway_ip': gateway_ip}) - data = jsonutils.dumps(route_info) - msg = ("Initiating POST request to configure route of " - "primary service at: %r" % mgmt_ip) - LOG.info(msg) - try: - resp = requests.post(url, data=data, timeout=self.timeout, - headers=headers) - except requests.exceptions.ConnectionError as err: - msg = ("Failed to establish connection to service at: " - "%r. ERROR: %r" % (mgmt_ip, str(err).capitalize())) - LOG.error(msg) - return msg - except requests.exceptions.RequestException as err: - msg = ("Unexpected ERROR happened while configuring " - "route of service at: %r ERROR: %r" % - (mgmt_ip, str(err).capitalize())) - LOG.error(msg) - return msg - - if resp.status_code in common_const.SUCCESS_CODES: - message = jsonutils.loads(resp.text) - if message.get("status", False): - msg = ("Route configured successfully for VYOS" - " service at: %r" % mgmt_ip) - LOG.info(msg) - active_configured = True - else: - msg = ("Configure source route failed on service with" - " status %s %s" - % (resp.status_code, message.get("reason", None))) - LOG.error(msg) - return msg - - msg = ("Route configuration status : %r " - % (active_configured)) - LOG.info(msg) - if active_configured: - return common_const.STATUS_SUCCESS - else: - return ("Failed to configure source route. Response code: %s." - "Response Content: %r" % (resp.status_code, resp.content)) - - def clear_routes(self, context, resource_data): - """ Clear routes for the service VM. - - Issues REST call to service VM for deletion of routes. - - :param context: neutron context - :param resource_data: a dictionary of vpn rules and objects - send by neutron plugin - - Returns: SUCCESS/Failure message with reason. - - """ - # clear the static stitching gateway route - headers = self._parse_vm_context(context) - resource_data = self.parse.parse_data(common_const.ROUTES, - resource_data) - mgmt_ip = resource_data.get('mgmt_ip') - source_cidrs = [resource_data.get('provider_cidr'), - resource_data.get('stitching_cidr')] - - stitching_url = const.request_url % (mgmt_ip, - const.CONFIGURATION_SERVER_PORT, - 'delete-stitching-route') - st_data = jsonutils.dumps( - {'gateway_ip': resource_data.get('stitching_gw_ip')}) - try: - resp = requests.post( - stitching_url, data=st_data, timeout=self.timeout, - headers=headers) - except requests.exceptions.ConnectionError as err: - msg = ("Failed to establish connection to service at: " - "%r. ERROR: %r" % (mgmt_ip, - str(err).capitalize())) - LOG.error(msg) - return msg - - active_configured = False - url = const.request_url % (mgmt_ip, const.CONFIGURATION_SERVER_PORT, - 'delete-source-route') - route_info = [] - for source_cidr in source_cidrs: - route_info.append({'source_cidr': source_cidr}) - data = jsonutils.dumps(route_info) - msg = ("Initiating DELETE route request to primary service at: %r" - % mgmt_ip) - LOG.info(msg) - try: - resp = requests.delete(url, data=data, timeout=self.timeout, - headers=headers) - except requests.exceptions.ConnectionError as err: - msg = ("Failed to establish connection to primary service at: " - " %r. ERROR: %r" % (mgmt_ip, err)) - LOG.error(msg) - return msg - except requests.exceptions.RequestException as err: - msg = ("Unexpected ERROR happened while deleting " - " route of service at: %r ERROR: %r" - % (mgmt_ip, err)) - LOG.error(msg) - return msg - - if resp.status_code in common_const.SUCCESS_CODES: - active_configured = True - - msg = ("Route deletion status : %r " - % (active_configured)) - LOG.info(msg) - if active_configured: - return common_const.STATUS_SUCCESS - else: - return ("Failed to delete source route. Response code: %s." - "Response Content: %r" % (resp.status_code, resp.content)) - - -@base_driver.set_class_attr(SERVICE_TYPE=vpn_const.SERVICE_TYPE, - SERVICE_VENDOR=const.SERVICE_VENDOR) -class VpnaasIpsecDriver(VpnGenericConfigDriver): - """ - Driver class for implementing VPN IPSEC configuration - requests from VPNaas Plugin. - """ - - def __init__(self, conf): - self.conf = conf - self.port = const.CONFIGURATION_SERVER_PORT - self.handlers = { - 'vpn_service': { - 'create': self.create_vpn_service}, - 'ipsec_site_connection': { - 'create': self.create_ipsec_conn, - 'update': self.update_ipsec_conn, - 'delete': self.delete_ipsec_conn}} - super(VpnaasIpsecDriver, self).__init__() - - def _update_conn_status(self, conn, status): - """ - Driver will call this API to report - status of a connection - only if there is any change. - :param conn: ipsec conn dicitonary - :param status: status of the service. - - Returns: updated status dictionary - """ - msg = ("Driver informing connection status " - "changed to %s" % status) - LOG.debug(msg) - vpnsvc_status = [{ - 'id': conn['vpnservice_id'], - 'status':'ACTIVE', - 'updated_pending_status':False, - 'ipsec_site_connections':{ - conn['id']: { - 'status': status, - 'updated_pending_status': True}}}] - return vpnsvc_status - - def _error_state(self, context, conn, message=''): - """ - Enqueues the status of the service to ERROR. - - :param context: Dictionary which holds all the required data for - for vpn service. - :param conn: ipsec conn dicitonary. - :param message: the cause for the error. - - Returns: None - """ - - self.agent.update_status( - context, self._update_conn_status(conn, - vpn_const.STATE_ERROR)) - msg = ("Resource ipsec site connection: %r went " - "to error state, %r" % (conn['id'], message)) - raise Exception(msg) - - def _init_state(self, context, conn): - """ - Enqueues the status of the service to ACTVIE. - - :param context: Dictionary which holds all the required data for - for vpn service. - :param conn: ipsec conn dicitonary. - - Returns: None - """ - msg = "IPSec: Configured successfully- %s " % conn['id'] - LOG.info(msg) - self.agent.update_status( - context, self._update_conn_status(conn, - vpn_const.STATE_INIT)) - - for item in context['service_info']['ipsec_site_conns']: - if item['id'] == conn['id']: - item['status'] = vpn_const.STATE_INIT - - def _ipsec_conn_correct_enc_algo(self, conn): - ike_enc_algo = conn['ikepolicy']['encryption_algorithm'] - ipsec_enc_algo = conn['ipsecpolicy']['encryption_algorithm'] - - algos = { - 'aes-128': "aes128", - 'aes-256': "aes256", - 'aes-192': "aes256"} - - if ike_enc_algo in list(algos.keys()): - ike_enc_algo = algos[ike_enc_algo] - if ipsec_enc_algo in list(algos.keys()): - ipsec_enc_algo = algos[ipsec_enc_algo] - - conn['ikepolicy']['encryption_algorithm'] = ike_enc_algo - conn['ipsecpolicy']['encryption_algorithm'] = ipsec_enc_algo - conn['ikepolicy']['name'] = ( - "ike-%s" % conn['ikepolicy']['id'].split('-')[0]) - conn['ipsecpolicy']['name'] = ( - "ipsec-%s" % conn['ikepolicy']['id'].split('-')[0]) - - def _get_filters(self, tenant_id=None, vpnservice_id=None, conn_id=None, - peer_address=None): - filters = {} - if tenant_id: - filters['tenant_id'] = tenant_id - if vpnservice_id: - filters['vpnservice_id'] = vpnservice_id - if conn_id: - filters['siteconn_id'] = conn_id - if peer_address: - filters['peer_address'] = peer_address - return filters - - def _get_stitching_cidr(self, conn): - # REVISIT: position based parsing of description - desc = conn['description'] - tokens = desc.split(';') - stitching_cidr = tokens[5].split('=')[1] - return stitching_cidr - - def _get_access_ip(self, conn): - svc_desc = conn['description'] - tokens = svc_desc.split(';') - access_ip = tokens[2].split('=')[1] - return access_ip - - def _get_ipsec_tunnel_local_cidr_from_vpnsvc(self, vpn_svc): - svc_desc = vpn_svc['description'] - tokens = svc_desc.split(';') - tunnel_local_cidr = tokens[1].split('=')[1] - - standby_fip = None - try: - standby_fip = tokens[9].split('=')[1] - except Exception: - pass - return tunnel_local_cidr, standby_fip - - def _get_ipsec_tunnel_local_cidr(self, svc_context): - return self._get_ipsec_tunnel_local_cidr_from_vpnsvc( - svc_context['service']) - - def _ipsec_create_conn(self, context, mgmt_fip, resource_data): - """ - Get the context for this ipsec conn and make POST to the service VM. - :param context: Dictionary which holds all the required data for - for vpn service. - :param mgmt_fip: managent floting ip - :paraM conn: ipsec conn dictionary - - Returns: None - """ - headers = self._parse_vm_context(context['agent_info']['context']) - conn = resource_data.get('resource') - description = conn['description'] - svc_context = self.agent.get_vpn_servicecontext( - context, self._get_filters(conn_id=conn['id']))[0] - dhgroup = {'group2': 2, - 'group5': 5, - 'group14': 14} - svc_context['siteconns'][0]['ikepolicy']['pfs_group'] = ( - dhgroup[svc_context['siteconns'][0]['ikepolicy']['pfs']]) - svc_context['siteconns'][0]['ipsecpolicy']['pfs_group'] = ( - dhgroup[svc_context['siteconns'][0]['ipsecpolicy']['pfs']]) - - # For cluster we need to send standby_fip to svc vm agent - tunnel_local_cidr, standby_fip = ( - self._get_ipsec_tunnel_local_cidr_from_vpnsvc(conn)) - if standby_fip: - svc_context['siteconns'][0]['connection']['standby_fip'] = ( - standby_fip) - conn = svc_context['siteconns'][0]['connection'] - conn['description'] = description - svc_context['siteconns'][0]['connection']['stitching_fixed_ip'] = ( - resource_data['stitching_ip']) - svc_context['siteconns'][0]['connection']['access_ip'] = ( - resource_data['stitching_floating_ip']) - msg = "IPSec: Pushing ipsec configuration %s" % conn - LOG.info(msg) - conn['tunnel_local_cidr'] = tunnel_local_cidr - self._ipsec_conn_correct_enc_algo(svc_context['siteconns'][0]) - peer_cidrs_from_2 = conn['peer_cidrs'][1:] - conn['peer_cidrs'] = [conn['peer_cidrs'][0]] - svc_context['service']['cidr'] = self._get_stitching_cidr(conn) - RestApi(mgmt_fip).post( - "create-ipsec-site-conn", svc_context, headers) - if peer_cidrs_from_2: - tunnel = {} - tunnel['peer_address'] = conn['peer_address'] - tunnel['local_cidr'] = tunnel_local_cidr - tunnel['peer_cidrs'] = peer_cidrs_from_2 - RestApi(mgmt_fip).post( - "create-ipsec-site-tunnel", tunnel, headers) - self._init_state(context, conn) - - def _ipsec_create_tunnel(self, context, mgmt_fip, conn): - """ - Get the context for this ipsec conn and make POST to the service VM. - :param context: Dictionary which holds all the required data for - for vpn service. - :param mgmt_fip: managent floting ip - :paraM conn: ipsec conn dictionary - - Returns: None - """ - headers = self._parse_vm_context(context['agent_info']['context']) - tunnel_local_cidr, _ = ( - self._get_ipsec_tunnel_local_cidr_from_vpnsvc(conn)) - - tunnel = {} - tunnel['peer_address'] = conn['peer_address'] - tunnel['local_cidr'] = tunnel_local_cidr - tunnel['peer_cidrs'] = conn['peer_cidrs'] - RestApi(mgmt_fip).post( - "create-ipsec-site-tunnel", tunnel, headers) - self._init_state(context, conn) - - def _ipsec_get_tenant_conns(self, context, mgmt_fip, conn, - on_delete=False): - """ - Get the context for this ipsec conn and vpn services. - - :param context: Dictionary which holds all the required data for - for vpn service. - :param mgmt_fip: managent floting ip - :paraM conn: ipsec conn dictionary - - Returns: list of ipsec conns - """ - - filters = { - 'tenant_id': [context['tenant_id']], - 'peer_address': [conn['peer_address']]} - tenant_conns = self.agent.get_ipsec_conns( - context, filters) - if not tenant_conns: - if not on_delete: - # Something went wrong - atleast the current - # connection should be there - msg = "No tenant conns for filters (%s)" % (str(filters)) - LOG.error(msg) - # Move conn into err state - self._error_state(context, conn, msg) - - conn_to_remove = None - - for connection in tenant_conns: - if connection['id'] == conn['id']: - conn_to_remove = connection - break - if conn_to_remove: - tenant_conns.remove(conn_to_remove) - if not tenant_conns: - return tenant_conns - - conn_list = [] - # get fip from connn description - access_ip = self._get_access_ip(conn) - svc_ids = [conn['vpnservice_id'] for conn in tenant_conns] - vpnservices = self.agent.get_vpn_services(context, ids=svc_ids) - copy_svc = copy.deepcopy(vpnservices) - # if service's fip matches new service's fip then both services - # lie on same instance, in this case we should only create tunnel - for vpn in copy_svc: - if access_ip in vpn['description']: - continue - else: - vpnservices.remove(vpn) - # we have all the vpnservices matching on this fip with same peer - for vpn in vpnservices: - # check any connection with same local(provider) subnet - matching_conn = [conn for conn in tenant_conns - if conn['vpnservice_id'] == vpn['id']] - conn_list.extend(matching_conn) - if not on_delete: - # Remove the conns which are in pending_create - # state. It might be possible that more than one - # conns could get created in database before the rpc - # method of dev driver is invoked. - # We have to separate first conn creation from rest. - copy_conns = copy.deepcopy(conn_list) - for tconn in copy_conns: - if tconn['status'] == ( - vpn_const.STATE_PENDING and tconn in conn_list): - conn_list.remove(tconn) - # conn_list is list of site connections which share same vpn_service. - return conn_list - - def _ipsec_check_overlapping_peer(self, context, - tenant_conns, conn): - pcidrs = conn['peer_cidrs'] - peer_address = conn['peer_address'] - for t_conn in tenant_conns: - t_pcidrs = t_conn['peer_cidrs'] - if conn['vpnservice_id'] != t_conn['vpnservice_id']: - continue - - for pcidr in pcidrs: - if (pcidr in t_pcidrs) and ( - not peer_address == t_conn['peer_address']): - msg = "Overlapping peer cidr (%s)" % (pcidr) - LOG.error(msg) - self._error_state( - context, conn, msg) - - def _ipsec_delete_tunnel(self, context, mgmt_fip, - resource_data): - """ - Make DELETE to the service VM to delete the tunnel. - - :param context: Dictionary which holds all the required data for - for vpn service. - :param mgmt_fip: managent floting ip - :paraM conn: ipsec conn dictionary - - Returns: None - """ - headers = self._parse_vm_context(context['agent_info']['context']) - conn = resource_data.get('resource') - lcidr = resource_data['provider_cidr'] - - tunnel = {} - tunnel['peer_address'] = conn['peer_address'] - tunnel['local_cidr'] = lcidr - tunnel['peer_cidrs'] = conn['peer_cidrs'] - try: - RestApi(mgmt_fip).delete( - "delete-ipsec-site-tunnel", tunnel, headers) - self.agent.ipsec_site_conn_deleted(context, conn['id']) - except Exception as err: - msg = ("IPSec: Failed to delete IPSEC tunnel. %s" - % str(err).capitalize()) - LOG.error(msg) - - def _ipsec_delete_connection(self, context, mgmt_fip, - conn): - """ - Make DELETE to the service VM to delete the ipsec conn. - - :param context: Dictionary which holds all the required data for - for vpn service. - :param mgmt_fip: managent floting ip - :paraM conn: ipsec conn dictionary - - Returns: None - """ - - try: - headers = self._parse_vm_context(context['agent_info']['context']) - RestApi(mgmt_fip).delete( - "delete-ipsec-site-conn", - {'peer_address': conn['peer_address']}, headers) - self.agent.ipsec_site_conn_deleted(context, conn['id']) - except Exception as err: - msg = ("IPSec: Failed to delete IPSEC conn. %s" - % str(err).capitalize()) - LOG.error(msg) - - def _ipsec_is_state_changed(self, context, svc_context, conn, fip): - """ - Make GET request to the service VM to get the status of the site conn. - - :param svc_context: list of ipsec conn dictionaries - :paraM conn: ipsec conn dictionary - :param fip: floting ip of the service VM - - Returns: None - """ - headers = self._parse_vm_context(context['agent_info']['context']) - c_state = None - lcidr, _ = self._get_ipsec_tunnel_local_cidr_from_vpnsvc(conn) - if conn['status'] == vpn_const.STATE_INIT: - tunnel = { - 'peer_address': conn['peer_address'], - 'local_cidr': lcidr, - 'peer_cidr': conn['peer_cidrs'][0]} - output = RestApi(fip).get( - "get-ipsec-site-tunnel-state", - tunnel, headers) - state = output['state'] - - if state.upper() == 'UP' and ( - conn['status'] != vpn_const.STATE_ACTIVE): - c_state = vpn_const.STATE_ACTIVE - if state.upper() == 'DOWN' and ( - conn['status'] == vpn_const.STATE_ACTIVE): - c_state = vpn_const.STATE_PENDING - - if c_state: - return c_state, True - return c_state, False - - def create_vpn_service(self, context, resource_data): - msg = "Validating VPN service %s " % resource_data.get('resource') - LOG.info(msg) - validator = VPNServiceValidator(self.agent) - validator.validate(context, resource_data) - - def create_ipsec_conn(self, context, resource_data): - """ - Implements functions to make update ipsec configuration in service VM. - - :param context: context dictionary of vpn service type - :param resource_data: dicionary of a specific operation type, - which was sent from neutron plugin - - Returns: None - """ - - conn = resource_data.get('resource') - mgmt_fip = resource_data['mgmt_ip'] - msg = "IPsec: create site connection %s" % conn - LOG.info(msg) - """ - Following conditions - - 0) Conn with more than one peer_address - is not allowed. This is because vyos has - conns and tunnels inside conn. But openstack - doesnt have tunnels. So conn will itslef need - to be mapped to tunnel. - a) Already conns exist for this tenant - . In this case just add a tunnel - . For same peer - . Add peer for different peer - b) First conn, create complete ipsec profile - """ - t_lcidr = resource_data['provider_cidr'] - if t_lcidr in conn['peer_cidrs']: - msg = ("IPSec: Tunnel remote cidr %s conflicts " - "with local cidr." % t_lcidr) - LOG.error(msg) - self._error_state(context, conn, msg) - if len(conn['peer_cidrs']) < 1: - msg = ("IPSec: Invalid number of peer CIDR. Should not be" - " less than 1.") - LOG.error(msg) - self._error_state(context, conn, msg) - - try: - tenant_conns = self._ipsec_get_tenant_conns( - context, mgmt_fip, conn) - except Exception as err: - msg = ("IPSec: Failed to get tenant conns for IPSEC create. %s" - % str(err).capitalize()) - LOG.error(msg) - self._error_state(context, conn, msg) - try: - """ - Check if this conn has overlapping peer - cidr with any other conn for the same - tenant - we do not support this model. - """ - self._ipsec_check_overlapping_peer( - context, tenant_conns, conn) - self._ipsec_create_conn(context, mgmt_fip, resource_data) - - except Exception as ex: - msg = "IPSec: Exception in creating ipsec conn: %s" % ex - LOG.error(msg) - self._error_state(context, conn, msg) - - def update_ipsec_conn(self, context, resource_data): - """ - Implements functions to make update ipsec configuration in service VM. - - :param context: context dictionary of vpn service type - :param resource_data: dicionary of a specific operation type, - which was sent from neutron plugin - - Returns: None - """ - pass - - def delete_ipsec_conn(self, context, resource_data): - """ - Implements function to make delete ipsec configuration in service VM. - - :param context: context dictionary of vpn service type - :param resource_data: dicionary of a specific operation type, - which was sent from neutron plugin - - Returns: None - """ - - conn = resource_data.get('resource') - msg = "IPsec: delete siteconnection %s" % conn - LOG.info(msg) - mgmt_fip = resource_data['mgmt_ip'] - - tenant_conns = self._ipsec_get_tenant_conns( - context, mgmt_fip, conn, on_delete=True) - try: - if tenant_conns: - self._ipsec_delete_tunnel( - context, mgmt_fip, resource_data) - else: - self._ipsec_delete_connection( - context, mgmt_fip, conn) - except Exception as ex: - msg = "IPSec: delete ipsec conn failed %s " % ex - LOG.error(msg) - self._error_state(context, conn, msg) - - def check_status(self, context, svc_context): - """ - Implements functions to get the status of the site to site conn. - - :param context: context dictionary of vpn service type - :param svc_contex: list of ipsec conn dictionaries - - Returns: None - """ - - vpn_desc = self.parse.parse_data(common_const.VPN, context) - # Other than non HA vpn_desc will be a list of parsed nfs - if type(vpn_desc) == list: - fip = vpn_desc[0]['mgmt_ip'] - else: - fip = vpn_desc['mgmt_ip'] - - conn = svc_context['siteconns'][0]['connection'] - - try: - state, changed = self._ipsec_is_state_changed(context, - svc_context, conn, fip) - except Exception as err: - msg = ("Failed to check if IPSEC state is changed. %s" - % str(err).capitalize()) - LOG.error(msg) - return vpn_const.STATE_ERROR - if changed: - self.agent.update_status( - context, self._update_conn_status(conn, - state)) - return state - - def vpnservice_updated(self, context, resource_data): - """ - Demultiplexes the different methods to update the configurations - - :param context: context dictionary of vpn service type - :param resource_data: dicionary of a specific operation type, - which was sent from neutron plugin - - Returns: None - """ - - vpn_desc = self.parse.parse_data(common_const.VPN, context) - resource_data.update(vpn_desc) - - msg = ("Handling VPN service update notification for '%s'" - % resource_data.get('reason', '')) - LOG.info(msg) - - resource = resource_data.get('resource') - tenant_id = resource['tenant_id'] - # Synchronize the update operation per tenant. - # Resources under tenant have inter dependencies. - - @lockutils.synchronized(tenant_id) - def _vpnservice_updated(context, resource_data): - reason = resource_data.get('reason') - rsrc = resource_data.get('rsrc_type') - - if rsrc not in list(self.handlers.keys()): - raise UnknownResourceException(rsrc=rsrc) - - if reason not in list(self.handlers[rsrc].keys()): - raise UnknownReasonException(reason=reason) - - self.handlers[rsrc][reason](context, resource_data) - - return _vpnservice_updated(context, resource_data) diff --git a/gbpservice/contrib/nfp/configurator/lib/__init__.py b/gbpservice/contrib/nfp/configurator/lib/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/configurator/lib/constants.py b/gbpservice/contrib/nfp/configurator/lib/constants.py deleted file mode 100644 index 6be7aad48..000000000 --- a/gbpservice/contrib/nfp/configurator/lib/constants.py +++ /dev/null @@ -1,55 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -supported_service_types = ['firewall', 'vpn', 'loadbalancerv2'] -NFP_SERVICE_LIST = ['heat', 'ansible'] -invalid_service_type = 'invalid' -NFP_SERVICE = 'nfp_service' -SUCCESS = 'SUCCESS' -FAILED = 'FAILED' -FAILURE = 'FAILURE' -GENERIC_CONFIG = 'generic_config' -ORCHESTRATOR = 'orchestrator' -EVENT_STASH = 'STASH_EVENT' -EVENT_PROCESS_BATCH = 'PROCESS_BATCH' -NFD_NOTIFICATION = 'network_function_device_notification' -RABBITMQ_HOST = '127.0.0.1' # send notifications to 'RABBITMQ_HOST' -NOTIFICATION_QUEUE = 'configurator-notifications' -FIREWALL = 'firewall' -VPN = 'vpn' -VYOS = 'vyos' -LOADBALANCERV2 = 'loadbalancerv2' -HAPROXY_LBAASV2 = 'haproxy' -CREATE = 'create' -UPDATE = 'update' -DELETE = 'delete' -POST = 'post' -PUT = 'put' -UNHANDLED = "UNHANDLED" - -HEALTHMONITOR = 'healthmonitor' -INTERFACES = 'interfaces' -ROUTES = 'routes' - -SUCCESS_CODES = [200, 201, 202, 203, 204] -ERROR_CODES = [400, 404, 500] - -STATUS_ACTIVE = "ACTIVE" -STATUS_DELETED = "DELETED" -STATUS_UPDATED = "UPDATED" -STATUS_ERROR = "ERROR" -STATUS_SUCCESS = "SUCCESS" -UNHANDLED = "UNHANDLED" -DOWN = "Down" - -AGENTS_PKG = ['gbpservice.contrib.nfp.configurator.agents'] -CONFIGURATOR_RPC_TOPIC = 'configurator' diff --git a/gbpservice/contrib/nfp/configurator/lib/data_filter.py b/gbpservice/contrib/nfp/configurator/lib/data_filter.py deleted file mode 100644 index 962e2e293..000000000 --- a/gbpservice/contrib/nfp/configurator/lib/data_filter.py +++ /dev/null @@ -1,239 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import copy - - -class Filter(object): - """ Filter class which provides data asked in a specific format. - - This class mocks all rpc calls going from *aaS agent/driver to respective - *aaS plugin. - """ - - def __init__(self, topic, default_version): - pass - - def call(self, context, msg): - """Returns data in specific format after applying filter on context - - :param context - :param msg e.g {'args': {'key': value,..},'method': 'function_name'}} - - Returns: data after applying filter on it - - """ - filters = {} - try: - for fk, fv in list(msg['args'].items()): - if dict == type(fv): - filters = fv - break - if fv: - filters = {fk: fv} - break - - method = getattr(self, '_%s' % (msg['method'])) - return method(context, filters) - except Exception as e: - raise e - - def make_msg(self, method, **kwargs): - """ Helper function needed to invoke Filter.call() - :param method - method name - :kwargs kwargs - filters to be used - - Returns: dict - """ - return {'method': method, - 'args': kwargs} - - def apply_filter(self, data, filters): - """ Apply filters on data - - :param filters e.g {k:[v],k:[v]} - :param data e.g [{k:v,k:v,k:v}, - {k:v,k:v,k:v}, - {k:v,k:v}] - - Returns: data after applying filter on it - - """ - - for fk, fv in list(filters.items()): - for d in data[:]: - if d.get(fk) is None: - data.remove(d) - if fk in d and d[fk] != fv[0]: - data.remove(d) - return data - - def get_record(self, data, key, value): - """Get single record based on key and value - - :param data - :praam key - :param value - - Returns: record - """ - for d in data: - if key in d and d[key] == value: - return d - - def _get_vpn_services(self, context, filters): - """ Get vpn service from context after applying filter - - :param context - vpn related resources - e.g context = {'service_info':{'vpnservices': [vpnservices], - 'ikepolicies': [ikepolicies], - 'ipsecpolicies':[ipsecpolicies], - 'ipsec_site_conns': - [ipsec_site_connections], - 'routers': [routers], - 'subnets': [subnets] - } - } - :param filters - e.g { 'ids' : [vpn service ids], - 'filters': filters } - - Returns: [vpn services] - - """ - vpn_ids = None - if 'ids' in filters and filters['ids']: - vpn_ids = filters['ids'] - service_info = context['service_info'] - vpnservices = service_info['vpnservices'] - filtered_vpns = [] - if vpn_ids: - for vpn_id in vpn_ids: - filtered_vpns.append( - self.get_record(vpnservices, 'id', vpn_id)) - return filtered_vpns - else: - return self.apply_filter(vpnservices, filters) - - def _get_ipsec_conns(self, context, filters): - """ Get ipsec site conns from context after applying filter - - :param context - vpn related resources - e.g context = {'service_info':{'vpnservices': [vpnservices], - 'ikepolicies': [ikepolicies], - 'ipsecpolicies':[ipsecpolicies], - 'ipsec_site_conns': - [ipsec_site_connections], - 'routers': [routers], - 'subnets': [subnets] - } - } - :param filters e.g { 'tenant_id': [tenant_id], - 'peer_address': [conn['peer_address']] - } - - Returns: [ipsec site conns] - - """ - service_info = context['service_info'] - ipsec_conns = copy.deepcopy(service_info['ipsec_site_conns']) - - return self.apply_filter(ipsec_conns, filters) - - def _get_vpn_servicecontext(self, context, filters): - """Get vpnservice context - - :param context - :param filters - - Returns IPSec site conns - """ - return self._get_ipsec_site2site_contexts(context, filters) - - def _get_ipsec_site2site_contexts(self, context, filters=None): - """ Get ipsec site to site context - :param filters - e.g {'tenant_id': , - 'vpnservice_id': , - 'siteconn_id': - } - 'tenant_id' - To get s2s conns of that tenant - 'vpnservice_id' - To get s2s conns of that vpn service - 'siteconn_id' - To get a specific s2s conn - - Returns: vpnservices - e.g { 'vpnserviceid': - { 'service': , - 'siteconns':[ { - 'connection': , - 'ikepolicy': , - 'ipsecpolicy': - } - ] - } - } - """ - if not filters: - filters = {} - - service_info = context['service_info'] - vpnservices = {} - s_filters = {} - if 'tenant_id' in filters: - s_filters['tenant_id'] = [filters['tenant_id']] - if 'vpnservice_id' in filters: - s_filters['vpnservice_id'] = [filters['vpnservice_id']] - if 'siteconn_id' in filters: - s_filters['id'] = [filters['siteconn_id']] - if 'peer_address' in filters: - s_filters['peer_address'] = [filters['peer_address']] - - ipsec_site_conns = self.apply_filter(service_info['ipsec_site_conns'], - s_filters) - - for conn in ipsec_site_conns: - - vpnservice = [vpn for vpn in service_info['vpnservices'] - if vpn['id'] == conn['vpnservice_id']][0] - - ikepolicy = [ikepolicy for ikepolicy in service_info['ikepolicies'] - if ikepolicy['id'] == conn['ikepolicy_id']][0] - - ipsecpolicy = [ipsecpolicy for ipsecpolicy in - service_info['ipsecpolicies'] - if ipsecpolicy['id'] == conn['ipsecpolicy_id']][0] - """ - Get the local subnet cidr - """ - - siteconn = {} - siteconn['connection'] = conn - siteconn['ikepolicy'] = ikepolicy - siteconn['ipsecpolicy'] = ipsecpolicy - vpnserviceid = vpnservice['id'] - - if vpnserviceid not in list(vpnservices.keys()): - vpnservices[vpnserviceid] = \ - {'service': vpnservice, 'siteconns': []} - - vpnservices[vpnserviceid]['siteconns'].append(siteconn) - - site2site_context = self._make_vpnservice_context(vpnservices) - return site2site_context - - def _make_vpnservice_context(self, vpnservices): - """Generate vpnservice context from the dictionary of vpnservices. - See, if some values are not needed by agent-driver, do not pass them. - As of now, passing everything. - """ - - return list(vpnservices.values()) diff --git a/gbpservice/contrib/nfp/configurator/lib/data_parser.py b/gbpservice/contrib/nfp/configurator/lib/data_parser.py deleted file mode 100644 index 8366cf2b9..000000000 --- a/gbpservice/contrib/nfp/configurator/lib/data_parser.py +++ /dev/null @@ -1,96 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import copy - -from gbpservice.nfp.common import constants as const -from gbpservice.nfp.core import log as nfp_logging - -LOG = nfp_logging.getLogger(__name__) - - -class DataParser(object): - ''' A library to parse device and service configuration and - transform them into a dictionary of key-value pairs - - ''' - - def __init__(self): - pass - - def parse_data(self, resource, data): - ''' Parser function exposed to the configurator modules. - - :param resource: Resource name (HEALTHMONITOR/INTERFACES/ROUTES/ - FIREWALL/LOADBALANCER/LOADBALANCERV2/VPN) - :param data: Resource data dictionary in case of device configuration - and context in case of service configuration - - Returns: a dictionary if nfds/nfs contains a single element else - a list of dictionaries where each dictionary corresponds - to each element in nfds/nfs - ''' - - config_data_list = [] - - if data.get('nfds'): - tenant_id = data['tenant_id'] - nf_config_list = data['nfds'] - elif data.get('resource_data'): - tenant_id = data['resource_data']['tenant_id'] - nf_config_list = data['resource_data']['nfs'] - else: - msg = ("The given schema of data dictionary is not supported " - "by the data parser library. Returning the input. " - "Input data is: %s" % data) - LOG.debug(msg) - return data - - for nf_config in nf_config_list: - self.resource_data = {} - self.resource_data.update({ - 'tenant_id': tenant_id, - 'role': nf_config['role'], - 'mgmt_ip': nf_config['svc_mgmt_fixed_ip']}) - - self._parse_config_data(nf_config, resource) - config_data_list.append(copy.deepcopy(self.resource_data)) - - return (config_data_list[0] - if len(config_data_list) == 1 - else config_data_list) - - def _parse_config_data(self, nfd, resource): - if resource.lower() == const.HEALTHMONITOR_RESOURCE: - return self.resource_data.update( - {'periodicity': nfd['periodicity'], - 'vmid': nfd['vmid']}) - - networks = nfd['networks'] - for network in networks: - prefix = network['type'] - port = network['ports'][0] - self.resource_data.update({ - (prefix + '_cidr'): network['cidr'], - (prefix + '_ip'): port['fixed_ip'], - (prefix + '_floating_ip'): port['floating_ip'], - (prefix + '_mac'): port['mac'], - (prefix + '_gw_ip'): network['gw_ip']}) - - vips = nfd.get('vips') - if not vips: - return - for vip in vips: - prefix = vip['type'] + '_vip' - self.resource_data.update({ - (prefix + '_ip'): vip['ip'], - (prefix + '_mac'): vip['mac']}) diff --git a/gbpservice/contrib/nfp/configurator/lib/demuxer.py b/gbpservice/contrib/nfp/configurator/lib/demuxer.py deleted file mode 100644 index 65231fb97..000000000 --- a/gbpservice/contrib/nfp/configurator/lib/demuxer.py +++ /dev/null @@ -1,165 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from gbpservice.contrib.nfp.configurator.lib import constants as const - - -class ServiceAgentDemuxer(object): - """Implements supporting methods for configurator module. - - Provides methods that take configurator API request data and helps - configurator to de-multiplex the API calls to different service agents - and drivers. - - Format of request data for network device configuration API: - request_data { - info { - version: - } - config [ - { - 'resource': , - 'kwargs': - }, - { - 'resource': , - 'kwargs': - }, ... - ] - } - Format of request data for network service configuration API: - request_data { - info { - version: - type: - } - config [ - { - 'resource': , - 'kwargs': - }, - { - 'resource': , - 'kwargs': - }, ... - ] - } - - """ - - def __init__(self): - pass - - def get_service_type(self, request_data): - """Retrieves service type from request data. - - :param request_data: API input data (format specified at top of file) - - Returns: - (1) "firewall"/"vpn"/"loadbalancer" - (2) "generic_config" if service_type field is absent in request_data - (3) "invalid" if any other service type is provided in request_data - - """ - - # Get service type based on the fact that for some request data - # formats the 'type' key is absent. Check for invalid types - service_type = request_data['info'].get('service_type').lower() - return service_type - - def get_service_agent_info(self, operation, resource_type, - request_data, is_generic_config): - """Prepares information for service agent consumption. - - :param operation: create/delete/update - :param resource_type: firewall/vpn/loadbalancer/generic_config - :param request_data: API input data (format specified at top of file) - - Returns: List with the following format. - sa_info_list [ - { - 'context': - 'resource_type': - 'method': <*aas RPC methods/generic configuration methods> - 'kwargs' - } - ] - - """ - - sa_info_list = [] - vendor_map = {const.FIREWALL: const.VYOS, - const.VPN: const.VYOS, - const.LOADBALANCERV2: const.HAPROXY_LBAASV2} - - service_vendor = request_data['info']['service_vendor'] - if str(service_vendor) == 'None': - service_vendor = vendor_map[resource_type] - - service_feature = request_data['info'].get('service_feature') - if not service_feature: - service_feature = '' - - for config_data in request_data['config']: - sa_info = {} - - resource_type_to_method_map = { - const.FIREWALL: (operation + '_' + config_data['resource']), - const.VPN: ('vpnservice_updated'), - const.LOADBALANCERV2: (operation + '_' + config_data[ - 'resource']), - const.NFP_SERVICE: ('run' + '_' + const.NFP_SERVICE), - const.GENERIC_CONFIG: { - const.CREATE: ('configure_' + config_data[ - 'resource']), - const.UPDATE: ('update_' + config_data['resource']), - const.DELETE: ('clear_' + config_data['resource'])}} - - context = request_data['info']['context'] - - data = config_data['resource_data'] - if not data: - return None - - resource = config_data['resource'] - is_nfp_svc = True if resource in const.NFP_SERVICE_LIST else False - - if is_generic_config: - method = resource_type_to_method_map[ - const.GENERIC_CONFIG][operation] - else: - if is_nfp_svc: - resource_type = const.NFP_SERVICE - try: - method = resource_type_to_method_map[resource_type] - except Exception: - method = 'handle_config' - - sa_info.update({'method': method, - 'resource_data': data, - 'agent_info': { - # This is the API context - 'context': context, - 'service_vendor': service_vendor.lower(), - 'service_feature': service_feature, - 'resource_type': resource_type.lower(), - 'resource': resource.lower()}, - 'is_generic_config': is_generic_config}) - - sa_info_list.append(sa_info) - - if is_nfp_svc: - resource_type = const.NFP_SERVICE - elif is_generic_config: - resource_type = const.GENERIC_CONFIG - - return sa_info_list, resource_type diff --git a/gbpservice/contrib/nfp/configurator/lib/filter_constants.py b/gbpservice/contrib/nfp/configurator/lib/filter_constants.py deleted file mode 100644 index a25830523..000000000 --- a/gbpservice/contrib/nfp/configurator/lib/filter_constants.py +++ /dev/null @@ -1,22 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -ACTIVE = "ACTIVE" -PENDING_CREATE = "PENDING_CREATE" -PENDING_UPDATE = "PENDING_UPDATE" -PENDING_DELETE = "PENDING_DELETE" -INACTIVE = "INACTIVE" -ACTIVE_PENDING_STATUSES = ( - ACTIVE, - PENDING_CREATE, - PENDING_UPDATE -) diff --git a/gbpservice/contrib/nfp/configurator/lib/fw_constants.py b/gbpservice/contrib/nfp/configurator/lib/fw_constants.py deleted file mode 100644 index 1c2288e62..000000000 --- a/gbpservice/contrib/nfp/configurator/lib/fw_constants.py +++ /dev/null @@ -1,17 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -SERVICE_TYPE = 'firewall' - -FIREWALL_CREATE_EVENT = 'CREATE_FIREWALL' -FIREWALL_UPDATE_EVENT = 'UPDATE_FIREWALL' -FIREWALL_DELETE_EVENT = 'DELETE_FIREWALL' diff --git a/gbpservice/contrib/nfp/configurator/lib/generic_config_constants.py b/gbpservice/contrib/nfp/configurator/lib/generic_config_constants.py deleted file mode 100644 index 57c9c77e6..000000000 --- a/gbpservice/contrib/nfp/configurator/lib/generic_config_constants.py +++ /dev/null @@ -1,35 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -SERVICE_TYPE = 'generic_config' -EVENT_CONFIGURE_INTERFACES = 'CONFIGURE_INTERFACES' -EVENT_CLEAR_INTERFACES = 'CLEAR_INTERFACES' -EVENT_CONFIGURE_ROUTES = 'CONFIGURE_ROUTES' -EVENT_CLEAR_ROUTES = 'CLEAR_ROUTES' -EVENT_CONFIGURE_HEALTHMONITOR = 'CONFIGURE_HEALTHMONITOR' -EVENT_CLEAR_HEALTHMONITOR = 'CLEAR_HEALTHMONITOR' - -# REVISIT: Need to make this configurable -MAX_FAIL_COUNT = 5 -INITIAL = 'initial' -FOREVER = 'forever' - -DEVICE_TO_BECOME_DOWN = 'DEVICE_TO_BECOME_DOWN' -DEVICE_TO_BECOME_UP = 'DEVICE_TO_BECOME_UP' -PERIODIC_HM = 'periodic_healthmonitor' - -DEVICE_NOT_REACHABLE = 'PERIODIC_HM_DEVICE_NOT_REACHABLE' -DEVICE_REACHABLE = 'PERIODIC_HM_DEVICE_REACHABLE' - -# POLLING EVENTS SPACING AND MAXRETRIES -EVENT_CONFIGURE_HEALTHMONITOR_SPACING = 10 # unit in sec. -EVENT_CONFIGURE_HEALTHMONITOR_MAXRETRY = 100 diff --git a/gbpservice/contrib/nfp/configurator/lib/lbv2_constants.py b/gbpservice/contrib/nfp/configurator/lib/lbv2_constants.py deleted file mode 100644 index 0b0d49caf..000000000 --- a/gbpservice/contrib/nfp/configurator/lib/lbv2_constants.py +++ /dev/null @@ -1,113 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -SERVICE_TYPE = 'loadbalancerv2' -NEUTRON = 'neutron' - -LBAAS_AGENT_RPC_TOPIC = 'lbaasv2_agent' -LBAAS_GENERIC_CONFIG_RPC_TOPIC = 'lbaas_generic_config' -LBAAS_PLUGIN_RPC_TOPIC = 'n-lbaas-plugin' -AGENT_TYPE_LOADBALANCER = 'OC Loadbalancer V2 agent' - -# Resources names -LOADBALANCER = 'loadbalancer' -LISTENER = 'listener' -POOL = 'pool' -MEMBER = 'member' -HEALTHMONITOR = 'healthmonitor' -SNI = 'sni' -L7POLICY = 'l7policy' -L7RULE = 'l7rule' -# Resources names for update apis -OLD_LOADBALANCER = 'old_loadbalancer' -OLD_LISTENER = 'old_listener' -OLD_POOL = 'old_pool' -OLD_MEMBER = 'old_member' -OLD_HEALTHMONITOR = 'old_healthmonitor' - -# Operations -CREATE = 'create' -UPDATE = 'update' -DELETE = 'delete' - -# Service operation status constants -ACTIVE = "ACTIVE" -DOWN = "DOWN" -CREATED = "CREATED" -PENDING_CREATE = "PENDING_CREATE" -PENDING_UPDATE = "PENDING_UPDATE" -PENDING_DELETE = "PENDING_DELETE" -INACTIVE = "INACTIVE" -ERROR = "ERROR" -STATUS_SUCCESS = "SUCCESS" - -ACTIVE_PENDING_STATUSES = ( - ACTIVE, - PENDING_CREATE, - PENDING_UPDATE -) - -REQUEST_URL = "http://%s:%s/%s" - -# Constants to extend status strings in neutron.plugins.common.constants -ONLINE = 'ONLINE' -OFFLINE = 'OFFLINE' -DEGRADED = 'DEGRADED' -DISABLED = 'DISABLED' -NO_MONITOR = 'NO_MONITOR' - -""" HTTP request/response """ -HTTP_REQ_METHOD_POST = 'POST' -HTTP_REQ_METHOD_GET = 'GET' -HTTP_REQ_METHOD_PUT = 'PUT' -HTTP_REQ_METHOD_DELETE = 'DELETE' -CONTENT_TYPE_HEADER = 'Content-type' -JSON_CONTENT_TYPE = 'application/json' - -LB_METHOD_ROUND_ROBIN = 'ROUND_ROBIN' -LB_METHOD_LEAST_CONNECTIONS = 'LEAST_CONNECTIONS' -LB_METHOD_SOURCE_IP = 'SOURCE_IP' - -PROTOCOL_TCP = 'TCP' -PROTOCOL_HTTP = 'HTTP' -PROTOCOL_HTTPS = 'HTTPS' - -HEALTH_MONITOR_PING = 'PING' -HEALTH_MONITOR_TCP = 'TCP' -HEALTH_MONITOR_HTTP = 'HTTP' -HEALTH_MONITOR_HTTPS = 'HTTPS' - -LBAAS = 'lbaas' - -""" Event ids """ -EVENT_CREATE_LOADBALANCER_V2 = 'CREATE_LOADBALANCER_V2' -EVENT_UPDATE_LOADBALANCER_V2 = 'UPDATE_LOADBALANCER_V2' -EVENT_DELETE_LOADBALANCER_V2 = 'DELETE_LOADBALANCER_V2' - -EVENT_CREATE_LISTENER_V2 = 'CREATE_LISTENER_V2' -EVENT_UPDATE_LISTENER_V2 = 'UPDATE_LISTENER_V2' -EVENT_DELETE_LISTENER_V2 = 'DELETE_LISTENER_V2' - -EVENT_CREATE_POOL_V2 = 'CREATE_POOL_V2' -EVENT_UPDATE_POOL_V2 = 'UPDATE_POOL_V2' -EVENT_DELETE_POOL_V2 = 'DELETE_POOL_V2' - -EVENT_CREATE_MEMBER_V2 = 'CREATE_MEMBER_V2' -EVENT_UPDATE_MEMBER_V2 = 'UPDATE_MEMBER_V2' -EVENT_DELETE_MEMBER_V2 = 'DELETE_MEMBER_V2' - -EVENT_CREATE_HEALTH_MONITOR_V2 = 'CREATE_HEALTH_MONITOR_V2' -EVENT_UPDATE_HEALTH_MONITOR_V2 = 'UPDATE_HEALTH_MONITOR_V2' -EVENT_DELETE_HEALTH_MONITOR_V2 = 'DELETE_HEALTH_MONITOR_V2' - -EVENT_AGENT_UPDATED_V2 = 'AGENT_UPDATED_V2' -EVENT_COLLECT_STATS_V2 = 'COLLECT_STATS_V2' diff --git a/gbpservice/contrib/nfp/configurator/lib/nfp_service_constants.py b/gbpservice/contrib/nfp/configurator/lib/nfp_service_constants.py deleted file mode 100644 index f97482301..000000000 --- a/gbpservice/contrib/nfp/configurator/lib/nfp_service_constants.py +++ /dev/null @@ -1,20 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -SERVICE_TYPE = 'nfp_service' -CREATE_NFP_SERVICE_EVENT = 'CREATE_NFP_SERVICE' -UNHANDLED_RESULT = 'unhandled' -ERROR_RESULT = 'error' -HEAT_RESOURCE = 'HEAT' -NFP_SERVICE = 'nfp_service' -SUCCESS = 'SUCCESS' -FAILURE = 'FAILURE' diff --git a/gbpservice/contrib/nfp/configurator/lib/utils.py b/gbpservice/contrib/nfp/configurator/lib/utils.py deleted file mode 100644 index 6dfd73726..000000000 --- a/gbpservice/contrib/nfp/configurator/lib/utils.py +++ /dev/null @@ -1,119 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import inspect -import os -import sys - -from gbpservice.nfp.core import log as nfp_logging - -LOG = nfp_logging.getLogger(__name__) - - -class ConfiguratorUtils(object): - """Utility class which provides common library functions for configurator. - - New common library functions, if needed, should be added in this class. - """ - - def __init__(self, conf): - self.conf = conf - - def load_drivers(self, service_type=None): - """Load all the driver class objects inside pkg. In each class in the - pkg it will look for keywork 'service_type' or/and 'vendor' and - select that class as driver class - - @param service_type: firewall/vpn/loadbalancer/nfp_service/None - - Returns: driver_objects dictionary - e.g driver_objects = {'loadbalancer': } - - """ - - pkgs = self.conf.CONFIG_DRIVERS.drivers - driver_objects = {} - modules = [] - subdirectories = [] - for pkg in pkgs: - base_driver = __import__(pkg, - globals(), locals(), ['drivers'], -1) - drivers_dir = base_driver.__path__[0] - subdirectories += [x[0] for x in os.walk(drivers_dir)] - - for subd in subdirectories: - syspath = sys.path - sys.path = [subd] + syspath - try: - files = os.listdir(subd) - except OSError: - msg = ("Failed to read files from dir %s" % (subd)) - LOG.error(msg) - files = [] - - for fname in files: - if fname.endswith(".py") and fname != '__init__.py': - modules += [__import__(fname[:-3])] - sys.path = syspath - - for module in modules: - for name, class_obj in inspect.getmembers(module): - if inspect.isclass(class_obj): - key = '' - if hasattr(class_obj, 'service_type') and ( - not service_type or (service_type.lower() in ( - class_obj.service_type.lower()))): - key += class_obj.service_type - else: - continue - if hasattr(class_obj, 'service_vendor'): - key += class_obj.service_vendor - if hasattr(class_obj, 'service_feature'): - key += class_obj.service_feature - if key: - driver_objects[key] = class_obj - - return driver_objects - - def load_agents(self, pkgs): - """Load all the agents inside pkg. - - @param pkg : package - e.g pkg = 'gbpservice.neutron.nsf.configurator.agents' - - Returns: imported_service_agents list - - """ - imported_service_agents = [] - pkgs = self.conf.CONFIG_AGENTS.agents - for pkg in pkgs: - base_agent = __import__(pkg, - globals(), locals(), ['agents'], -1) - agents_dir = base_agent.__path__[0] - syspath = sys.path - sys.path = [agents_dir] + syspath - try: - files = os.listdir(agents_dir) - except OSError: - msg = ("Failed to read files from dir %s" % (agents_dir)) - LOG.error(msg) - files = [] - - for fname in files: - if fname.endswith(".py") and fname != '__init__.py': - agent = __import__(pkg, globals(), - locals(), [fname[:-3]], -1) - imported_service_agents += [ - eval('agent.%s' % (fname[:-3]))] - # modules += [__import__(fname[:-3])] - sys.path = syspath - return imported_service_agents diff --git a/gbpservice/contrib/nfp/configurator/lib/vpn_constants.py b/gbpservice/contrib/nfp/configurator/lib/vpn_constants.py deleted file mode 100644 index b65f5586f..000000000 --- a/gbpservice/contrib/nfp/configurator/lib/vpn_constants.py +++ /dev/null @@ -1,25 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -SERVICE_TYPE = 'vpn' - - -STATE_PENDING = 'PENDING_CREATE' -STATE_INIT = 'INIT' -STATE_ACTIVE = 'ACTIVE' -STATE_ERROR = 'ERROR' - - -VPN_GENERIC_CONFIG_RPC_TOPIC = "vyos_vpn_topic" - -VPN_PLUGIN_TOPIC = 'vpn_plugin' -VPN_AGENT_TOPIC = 'vpn_agent' diff --git a/gbpservice/contrib/nfp/configurator/modules/configurator.py b/gbpservice/contrib/nfp/configurator/modules/configurator.py deleted file mode 100644 index 9ba2aa1d2..000000000 --- a/gbpservice/contrib/nfp/configurator/modules/configurator.py +++ /dev/null @@ -1,542 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from oslo_log import helpers as log_helpers - -from gbpservice.contrib.nfp.configurator.lib import constants as const -from gbpservice.contrib.nfp.configurator.lib import demuxer -from gbpservice.contrib.nfp.configurator.lib import utils -from gbpservice.nfp.core import context as module_context -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.core import rpc - - -LOG = nfp_logging.getLogger(__name__) - - -class ConfiguratorRpcManager(object): - """Implements procedure calls invoked by an REST server. - - Implements following RPC methods. - - create_network_function_device_config - - delete_network_function_device_config - - update_network_function_device_config - - create_network_function_config - - delete_network_function_config - - update_network_function_config - - get_notifications - Also implements local methods for supporting RPC methods - - """ - - def __init__(self, sc, cm, conf, demuxer): - self.sc = sc - self.cm = cm - self.conf = conf - self.demuxer = demuxer - - def _get_service_agent_instance(self, service_type): - """Provides service agent instance based on service type. - - :param service_type: firewall/vpn/loadbalancer/generic_config - - Returns: Instance of service agent for a given service type - - """ - - return self.cm.sa_instances[service_type] - - def _invoke_service_agent(self, operation, - request_data, is_generic_config=False): - """Maps and invokes an RPC call to a service agent method. - - Takes help of de-multiplexer to get service type and corresponding - data and invokes the method of service agent. Service agent instance - is identified based on the service type passed in the request data - - :param operation: Operation type - create/delete/update - :param request_data: RPC data - - Returns: None - - """ - - # Retrieves service type from RPC data - service_type = self.demuxer.get_service_type(request_data) - if (const.invalid_service_type == service_type): - msg = ("Configurator received invalid service type %s." % - service_type) - raise Exception(msg) - - # Retrieves service agent information from RPC data - # Format of sa_req_list: - # [{'method': , 'kwargs': }, {}, ... ] - sa_req_list, service_type = self.demuxer.get_service_agent_info( - operation, service_type, - request_data, is_generic_config) - if not sa_req_list: - msg = ("Configurator received invalid data format for service" - " type %s. Data format: %r" % (service_type, request_data)) - raise Exception(msg) - - # Retrieves service agent instance using service type - sa_instance = self._get_service_agent_instance(service_type) - if not sa_instance: - msg = ("Failed to find agent with service type %s." % service_type) - raise Exception(msg) - - # Notification data list that needs to be returned after processing - # RPC request. Format of notification data: - # notification_data[ - # { - # 'receiver': , - # 'resource': , - # 'method': , - # 'kwargs': [{}, {data2}] - # }, - # { - # }, ... - # ] - # - # Initially, notification data will be empty and is populated - # after processing each request data in the request data list - notification_data = {} - - # Handover the request data list and notification data to the - # identified service agent - sa_instance.process_request(sa_req_list, notification_data) - - @log_helpers.log_method_call - def create_network_function_device_config(self, context, request_data): - """RPC method to configure a network service device. - - Configures a network service VM to facilitate network service - operation. This RPC method is invoked by the configurator REST - server. It configures a network service based on the configuration - request specified in the request_data argument. - - :param context: RPC context instance - :param request_data: RPC data - - Returns: None - - """ - - try: - nfp_context = module_context.init() - log_info = request_data.get('info') - logging_context = log_info['context'].get('logging_context', {}) - nfp_context['log_context'] = logging_context - LOG.info("Received RPC CREATE NETWORK FUNCTION DEVICE CONFIG " - "for %(service_type)s, NFI: %(nfi)s, " - "NF_ID: %(nf_id)s", - {'service_type': request_data['info']['service_type'], - 'nfi': request_data['info']['context']['nfi_id'], - 'nf_id': request_data['info']['context']['nf_id']}) - - self._invoke_service_agent('create', request_data, True) - except Exception as err: - msg = ("Failed to create network device configuration. %s" % - str(err).capitalize()) - LOG.error(msg) - - @log_helpers.log_method_call - def delete_network_function_device_config(self, context, request_data): - """RPC method to clear configuration of a network service device. - - Clears configuration of a network service VM. This RPC method is - invoked by the configurator REST server. It clears configuration - of a network service based on the configuration request specified - in the request_data argument. - - :param context: RPC context instance - :param request_data: RPC data - - Returns: None - - """ - - try: - nfp_context = module_context.init() - log_info = request_data.get('info') - logging_context = log_info['context'].get('logging_context', {}) - nfp_context['log_context'] = logging_context - LOG.info("Received RPC DELETE NETWORK FUNCTION DEVICE CONFIG " - "for %(service_type)s, NFI: %(nfi)s, " - "NF_ID: %(nf_id)s", - {'service_type': request_data['info']['service_type'], - 'nfi': request_data['info']['context']['nfi_id'], - 'nf_id': request_data['info']['context']['nf_id']}) - - self._invoke_service_agent('delete', request_data, True) - except Exception as err: - msg = ("Failed to delete network device configuration. %s" % - str(err).capitalize()) - LOG.error(msg) - - @log_helpers.log_method_call - def update_network_function_device_config(self, context, request_data): - """RPC method to update of configuration in a network service device. - - Updates configuration of a network service VM. This RPC method is - invoked by the configurator REST server. It updates configuration - of a network service based on the configuration request specified - in the request_data argument. - - :param context: RPC context instance - :param request_data: RPC data - - Returns: None - - """ - - try: - nfp_context = module_context.init() - log_info = request_data.get('info') - logging_context = log_info['context'].get('logging_context', {}) - nfp_context['log_context'] = logging_context - LOG.info("Received RPC UPDATE NETWORK FUNCTION DEVICE CONFIG " - "for %(service_type)s, NFI: %(nfi)s, " - "NF_ID: %(nf_id)s", - {'service_type': request_data['info']['service_type'], - 'nfi': request_data['info']['context']['nfi_id'], - 'nf_id': request_data['info']['context']['nf_id']}) - - self._invoke_service_agent('update', request_data, True) - except Exception as err: - msg = ("Failed to update network device configuration. %s" % - str(err).capitalize()) - LOG.error(msg) - - @log_helpers.log_method_call - def create_network_function_config(self, context, request_data): - """RPC method to configure a network service. - - Configures a network service specified in the request data. This - RPC method is invoked by the configurator REST server. It configures - a network service based on the configuration request specified in - the request_data argument. - - :param context: RPC context instance - :param request_data: RPC data - - Returns: None - - """ - - try: - nfp_context = module_context.init() - log_info = request_data.get('info') - logging_context = log_info['context'].get('logging_context', {}) - nfp_context['log_context'] = logging_context - LOG.info("Received RPC CREATE NETWORK FUNCTION CONFIG " - "for %(service_type)s ", - {'service_type': request_data['info']['service_type']}) - - self._invoke_service_agent('create', request_data) - except Exception as err: - msg = ("Failed to create network service configuration. %s" % - str(err).capitalize()) - LOG.error(msg) - - @log_helpers.log_method_call - def delete_network_function_config(self, context, request_data): - """RPC method to clear configuration of a network service. - - Clears configuration of a network service. This RPC method is - invoked by the configurator REST server. It clears configuration - of a network service based on the configuration request specified - in the request_data argument. - - :param context: RPC context instance - :param request_data: RPC data - - Returns: None - - """ - - try: - nfp_context = module_context.init() - log_info = request_data.get('info') - logging_context = log_info['context'].get('logging_context', {}) - nfp_context['log_context'] = logging_context - LOG.info("Received RPC DELETE NETWORK FUNCTION CONFIG " - "for %(service_type)s ", - {'service_type': request_data['info']['service_type']}) - - self._invoke_service_agent('delete', request_data) - except Exception as err: - msg = ("Failed to delete network service configuration. %s" % - str(err).capitalize()) - LOG.error(msg) - - @log_helpers.log_method_call - def update_network_function_config(self, context, request_data): - """RPC method to update of configuration in a network service. - - Updates configuration of a network service. This RPC method is - invoked by the configurator REST server. It updates configuration - of a network service based on the configuration request specified - in the request_data argument. - - :param context: RPC context instance - :param request_data: RPC data - - Returns: None - - """ - - try: - nfp_context = module_context.init() - log_info = request_data.get('info') - logging_context = log_info['context'].get('logging_context', {}) - nfp_context['log_context'] = logging_context - LOG.info("Received RPC UPDATE NETWORK FUNCTION CONFIG " - "for %(service_type)s ", - {'service_type': request_data['info']['service_type']}) - - self._invoke_service_agent('update', request_data) - except Exception as err: - msg = ("Failed to update network service configuration. %s" % - str(err).capitalize()) - LOG.error(msg) - - @log_helpers.log_method_call - def get_notifications(self, context): - """RPC method to get all notifications published by configurator. - - Gets all the notifications from the notifications from notification - queue and sends to configurator agent - - :param context: RPC context instance - - Returns: notification_data - - """ - module_context.init() - LOG.info("Received RPC GET NOTIFICATIONS ") - events = self.sc.get_stashed_events() - notifications = [] - for event in events: - notification = event.data - msg = ("Notification Data: %r" % notification) - notifications.append(notification) - LOG.info(msg) - return notifications - - -class ConfiguratorModule(object): - """Implements configurator module APIs. - - Implements methods which are either invoked by registered service - agents or by the configurator global methods. The methods invoked - by configurator global methods interface with service agents. - - """ - - def __init__(self, sc): - self.sa_instances = {} - self.imported_sas = [] - - def register_service_agent(self, service_type, service_agent): - """Stores service agent object. - - :param service_type: Type of service - firewall/vpn/loadbalancer/ - generic_config. - :param service_agent: Instance of service agent class. - - Returns: Nothing - - """ - - if service_type not in self.sa_instances: - - msg = ("Configurator registered service agent of type %s." % - service_type) - LOG.info(msg) - else: - msg = ("Identified duplicate registration with service type %s." % - service_type) - LOG.warning(msg) - - # Register the service agent irrespective of previous registration - self.sa_instances.update({service_type: service_agent}) - - def init_service_agents(self, sc, conf): - """Invokes service agent initialization method. - - :param sc: Service Controller object that is used for interfacing - with core service controller. - :param conf: Configuration object that is used for configuration - parameter access. - - Returns: None - - """ - - for agent in self.imported_sas: - try: - agent.init_agent(self, sc, conf) - except AttributeError as attr_err: - LOG.error(agent.__dict__) - raise AttributeError(agent.__file__ + ': ' + str(attr_err)) - - def init_service_agents_complete(self, sc, conf): - """Invokes service agent initialization complete method. - - :param sc: Service Controller object that is used for interfacing - with core service controller. - :param conf: Configuration object that is used for configuration - parameter access. - - Returns: None - - """ - - for agent in self.imported_sas: - try: - agent.init_agent_complete(self, sc, conf) - except AttributeError as attr_err: - LOG.error(agent.__dict__) - raise AttributeError(agent.__file__ + ': ' + str(attr_err)) - - -def init_rpc(sc, cm, conf, demuxer): - """Initializes oslo RPC client. - - Creates RPC manager object and registers the configurator's RPC - agent object with core service controller. - - :param sc: Service Controller object that is used for interfacing - with core service controller. - :param cm: Configurator module object that is used for accessing - ConfiguratorModule class methods. - :param conf: Configuration object that is used for configuration - parameter access. - :param demuxer: De-multiplexer object that is used for accessing - ServiceAgentDemuxer class methods. - - Returns: None - - """ - - # Initializes RPC client - rpc_mgr = ConfiguratorRpcManager(sc, cm, conf, demuxer) - configurator_agent = rpc.RpcAgent(sc, - topic=const.CONFIGURATOR_RPC_TOPIC, - manager=rpc_mgr) - - # Registers RPC client object with core service controller - sc.register_rpc_agents([configurator_agent]) - - -def get_configurator_module_instance(sc, conf): - """ Provides ConfiguratorModule class object and loads service agents. - - Returns: Instance of ConfiguratorModule class - - """ - - cm = ConfiguratorModule(sc) - conf_utils = utils.ConfiguratorUtils(conf) - - # Loads all the service agents under AGENT_PKG module path - cm.imported_sas = conf_utils.load_agents(const.AGENTS_PKG) - msg = ("Configurator loaded service agents from %s location." - % (cm.imported_sas)) - LOG.info(msg) - return cm - - -def nfp_module_init(sc, conf): - """Initializes configurator module. - - Creates de-multiplexer object and invokes all the agent entry point - functions. Initializes oslo RPC client for receiving messages from - REST server. Exceptions are raised to parent function for all types - of failures. - - :param sc: Service Controller object that is used for interfacing - with core service controller. - :param conf: Configuration object that is used for configuration - parameter access. - - Returns: None - Raises: Generic exception including error message - - """ - - # Create configurator module and de-multiplexer objects - try: - cm = get_configurator_module_instance(sc, conf) - demuxer_instance = demuxer.ServiceAgentDemuxer() - except Exception as err: - msg = ("Failed to initialize configurator de-multiplexer. %s." - % (str(err).capitalize())) - LOG.error(msg) - raise Exception(err) - else: - msg = ("Initialized configurator de-multiplexer.") - LOG.info(msg) - - # Initialize all the pre-loaded service agents - try: - cm.init_service_agents(sc, conf) - except Exception as err: - msg = ("Failed to initialize configurator agent modules. %s." - % (str(err).capitalize())) - LOG.error(msg) - raise Exception(err) - else: - msg = ("Initialized configurator agents.") - LOG.info(msg) - - # Initialize RPC client for receiving messages from REST server - try: - init_rpc(sc, cm, conf, demuxer_instance) - except Exception as err: - msg = ("Failed to initialize configurator RPC with topic %s. %s." - % (const.CONFIGURATOR_RPC_TOPIC, str(err).capitalize())) - LOG.error(msg) - raise Exception(err) - else: - msg = ("Initialized configurator RPC with topic %s." - % const.CONFIGURATOR_RPC_TOPIC) - LOG.debug(msg) - - -def nfp_module_post_init(sc, conf): - """Invokes service agent's initialization complete methods. - - :param sc: Service Controller object that is used for interfacing - with core service controller. - :param conf: Configuration object that is used for configuration - parameter access. - - Returns: None - Raises: Generic exception including error message - - """ - - try: - cm = get_configurator_module_instance(sc, conf) - cm.init_service_agents_complete(sc, conf) - except Exception as err: - msg = ("Failed to trigger initialization complete for configurator" - " agent modules. %s." % (str(err).capitalize())) - LOG.error(msg) - raise Exception(err) - else: - msg = ("Initialization of configurator agent modules completed.") - LOG.info(msg) diff --git a/gbpservice/contrib/nfp/service_plugins/__init__.py b/gbpservice/contrib/nfp/service_plugins/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/service_plugins/firewall/__init__.py b/gbpservice/contrib/nfp/service_plugins/firewall/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/service_plugins/firewall/nfp_fwaas_plugin.py b/gbpservice/contrib/nfp/service_plugins/firewall/nfp_fwaas_plugin.py deleted file mode 100644 index 17a28e0e5..000000000 --- a/gbpservice/contrib/nfp/service_plugins/firewall/nfp_fwaas_plugin.py +++ /dev/null @@ -1,326 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from keystoneclient import exceptions as k_exceptions -from keystoneclient.v2_0 import client as keyclient - -from gbpservice._i18n import _ -from gbpservice.common import utils -from gbpservice.contrib.nfp.config_orchestrator.common import topics -from gbpservice.nfp.core import log as nfp_logging -import netaddr - -from neutron.db import l3_db -from neutron.db.l3_db import DEVICE_OWNER_ROUTER_INTF -from neutron.db.l3_db import EXTERNAL_GW_INFO -from neutron.db.models.l3 import RouterPort -from neutron.db import models_v2 -from neutron_lib import constants as nlib_const -from neutron_lib import exceptions as n_exc -from neutron_lib.exceptions import l3 -from neutron_lib.plugins import constants as n_const - -import neutron_fwaas.extensions -from neutron_fwaas.services.firewall import fwaas_plugin as ref_fw_plugin -from oslo_config import cfg -from oslo_utils import excutils -from oslo_utils import uuidutils -from sqlalchemy import orm - -from neutron_fwaas.db.firewall import firewall_db as n_firewall - -LOG = nfp_logging.getLogger(__name__) - - -class NFPFirewallPlugin(ref_fw_plugin.FirewallPlugin): - def __init__(self): - # Monkey patch L3 agent topic - # L3 agent was where reference firewall agent runs - # patch that topic to the NFP firewall agent's topic name - ref_fw_plugin.f_const.L3_AGENT = topics.FW_NFP_CONFIGAGENT_TOPIC - - # Ensure neutron fwaas extensions are loaded - ext_path = neutron_fwaas.extensions.__path__[0] - if ext_path not in cfg.CONF.api_extensions_path.split(':'): - cfg.CONF.set_override( - 'api_extensions_path', - cfg.CONF.api_extensions_path + ':' + ext_path) - - super(NFPFirewallPlugin, self).__init__() - - # Modifying following plugin function, to relax same router validation - def _get_routers_for_create_firewall(self, tenant_id, context, firewall): - - # pop router_id as this goes in the router association db - # and not firewall db - router_ids = firewall['firewall'].pop('router_ids', None) - if router_ids == nlib_const.ATTR_NOT_SPECIFIED: - return tenant_id - - def set_routers_for_firewall(self, context, fw): - """Sets the routers associated with the fw.""" - pass - - def get_firewall_routers(self, context, fwid): - """Gets all routers associated with a firewall.""" - fw_rtrs = ['1234567890'] - return fw_rtrs - - def validate_firewall_routers_not_in_use( - self, context, router_ids, fwid=None): - """Validate if router-ids not associated with any firewall. - - If any of the router-ids in the list is already associated with - a firewall, raise an exception else just return. - """ - pass - - def update_firewall_routers(self, context, fw): - """Update the firewall with new routers. - - This involves removing existing router associations and replacing - it with the new router associations provided in the update method. - """ - return fw - - -# Monkey patching the create_firewall db method -def create_firewall(self, context, firewall, status=None): - fw = firewall['firewall'] - tenant_id = fw['tenant_id'] - # distributed routers may required a more complex state machine; - # the introduction of a new 'CREATED' state allows this, whilst - # keeping a backward compatible behavior of the logical resource. - if not status: - status = n_const.PENDING_CREATE - with context.session.begin(subtransactions=True): - self._validate_fw_parameters(context, fw, tenant_id) - firewall_db = n_firewall.Firewall( - id=uuidutils.generate_uuid(), - tenant_id=tenant_id, - name=fw['name'], - description=fw['description'], - firewall_policy_id=fw['firewall_policy_id'], - admin_state_up=fw['admin_state_up'], - status=status) - context.session.add(firewall_db) - return self._make_firewall_dict(firewall_db) - - -n_firewall.Firewall_db_mixin.create_firewall = create_firewall - - -# Monkey patching l3_db's _get_router_for_floatingip method to associate -# floatingip if corresponding routes is present. -def _is_net_reachable_from_net(self, context, tenant_id, from_net_id, - to_net_id): - """Check whether a network is reachable. - - Follow the paths of networks connected by devices, to determine - whether a network is reachable from another. - @param context: neutron api request context - @param tenant_id: the owning tenant - @param from_net_id: the source network for the search - @param to_net_id: the destination network for the search - @return: True or False whether a path exists - """ - original_context = context - context = elevate_context(context) - tenant_id = context.tenant_id - - def nexthop_nets_query(nets, visited): - """query networks connected to devices on nets but not visited.""" - Port = models_v2.Port - devices_on_nets = context.session.query(Port.device_id).filter( - Port.tenant_id == tenant_id, - Port.device_owner.notin_([nlib_const.DEVICE_OWNER_DHCP]), - Port.network_id.in_(nets)).subquery() - return context.session.query(Port.network_id).filter( - Port.tenant_id == tenant_id, - Port.network_id.notin_(visited), - Port.device_id.in_(devices_on_nets)) - visited = set([]) - nets = set([from_net_id]) - while nets: - if to_net_id in nets: - context = original_context - return True - visited |= nets - nets = set((tup[0] for tup in nexthop_nets_query(nets, visited))) - context = original_context - return False - - -def _find_net_for_nexthop(self, context, tenant_id, router_id, nexthop): - """Find the network to which the nexthop belongs. - - Iterate over the router interfaces to find the network of nexthop. - @param context: neutron api request context - @param tenant_id: the owning tenant - @param router_id: a router id - @param nexthop: an IP address - @return: the network id of the nexthop or None if not found - """ - interfaces = context.session.query(models_v2.Port).filter_by( - tenant_id=tenant_id, - device_id=router_id, - device_owner=DEVICE_OWNER_ROUTER_INTF) - for interface in interfaces: - cidrs = [self._core_plugin._get_subnet(context, - ip['subnet_id'])['cidr'] - for ip in interface['fixed_ips']] - if netaddr.all_matching_cidrs(nexthop, cidrs): - return interface['network_id'] - - -def _find_routers_via_routes_for_floatingip(self, context, internal_port, - internal_subnet_id, - external_network_id): - """Find routers with route to the internal IP address. - - Iterate over the routers that belong to the same tenant as - 'internal_port'. For each router check that the router is connected - to the external network and whether there is a route to the internal - IP address. Consider only routers for which there is a path from the - nexthop of the route to the internal port. - - Sort the list of routers to have the router with the most specific - route first (largest CIDR prefix mask length). - - @param context: neutron api request context - @param internal_port: the port dict for the association - @param internal_subnet_id: the subnet for the association - @param external_network_id: the network of the floatingip - @return: a sorted list of matching routers - """ - original_context = context - context = elevate_context(context) - internal_ip_address = [ - ip['ip_address'] for ip in internal_port['fixed_ips'] - if ip['subnet_id'] == internal_subnet_id - ][0] - - # find the tenant routers - tenant_id = internal_port['tenant_id'] - routers = self.get_routers(context, filters={'tenant_id': [tenant_id]}) - - prefix_routers = [] - for router in routers: - # verify that the router is on "external_network" - gw_info = router.get(EXTERNAL_GW_INFO) - if not gw_info or gw_info['network_id'] != external_network_id: - continue - # find a matching route - if 'routes' not in router: - continue - cidr_nexthops = {} - for route in router['routes']: - cidr = netaddr.IPNetwork(route['destination']) - if cidr not in cidr_nexthops: - cidr_nexthops[cidr] = [] - cidr_nexthops[cidr].append(route['nexthop']) - smallest_cidr = netaddr.smallest_matching_cidr( - internal_ip_address, - list(cidr_nexthops.keys())) - if not smallest_cidr: - continue - # validate that there exists a path to "internal_port" - for nexthop in cidr_nexthops[smallest_cidr]: - net_id = self._find_net_for_nexthop(context, context.tenant_id, - router['id'], nexthop) - if net_id and self._is_net_reachable_from_net( - context, - context.tenant_id, - net_id, - internal_port['network_id']): - prefix_routers.append( - (smallest_cidr.prefixlen, router['id'])) - break - context = original_context - return [p_r[1] for p_r in sorted(prefix_routers, reverse=True)] - - -def elevate_context(context): - context = context.elevated() - context.tenant_id = _resource_owner_tenant_id() - return context - - -def _resource_owner_tenant_id(): - user, pwd, tenant, auth_url = utils.get_keystone_creds() - keystoneclient = keyclient.Client(username=user, password=pwd, - auth_url=auth_url) - try: - tenant = keystoneclient.tenants.find(name=tenant) - return tenant.id - except k_exceptions.NotFound: - with excutils.save_and_reraise_exception(reraise=True): - LOG.error('No tenant with name %s exists.', tenant) - except k_exceptions.NoUniqueMatch: - with excutils.save_and_reraise_exception(reraise=True): - LOG.error('Multiple tenants matches found for %s', tenant) - - -def _get_router_for_floatingip(self, context, internal_port, - internal_subnet_id, - external_network_id): - subnet = self._core_plugin.get_subnet(context, internal_subnet_id) - if not subnet['gateway_ip']: - msg = (_('Cannot add floating IP to port on subnet %s ' - 'which has no gateway_ip') % internal_subnet_id) - raise n_exc.BadRequest(resource='floatingip', msg=msg) - - # Find routers(with router_id and interface address) that - # connect given internal subnet and the external network. - # Among them, if the router's interface address matches - # with subnet's gateway-ip, return that router. - # Otherwise return the first router. - gw_port = orm.aliased(models_v2.Port, name="gw_port") - routerport_qry = context.session.query( - RouterPort.router_id, models_v2.IPAllocation.ip_address).join( - models_v2.Port, models_v2.IPAllocation).filter( - models_v2.Port.network_id == internal_port['network_id'], - RouterPort.port_type.in_(nlib_const.ROUTER_INTERFACE_OWNERS), - models_v2.IPAllocation.subnet_id == internal_subnet_id - ).join(gw_port, gw_port.device_id == RouterPort.router_id).filter( - gw_port.network_id == external_network_id).distinct() - - first_router_id = None - for router_id, interface_ip in routerport_qry: - if interface_ip == subnet['gateway_ip']: - return router_id - if not first_router_id: - first_router_id = router_id - if first_router_id: - return first_router_id - - router_ids = self._find_routers_via_routes_for_floatingip( - context, - internal_port, - internal_subnet_id, - external_network_id) - if router_ids: - return router_ids[0] - - raise l3.ExternalGatewayForFloatingIPNotFound( - subnet_id=internal_subnet_id, - external_network_id=external_network_id, - port_id=internal_port['id']) - - -l3_db.L3_NAT_dbonly_mixin._get_router_for_floatingip = ( - _get_router_for_floatingip) -l3_db.L3_NAT_dbonly_mixin._find_routers_via_routes_for_floatingip = ( - _find_routers_via_routes_for_floatingip) -l3_db.L3_NAT_dbonly_mixin._find_net_for_nexthop = _find_net_for_nexthop -l3_db.L3_NAT_dbonly_mixin._is_net_reachable_from_net = ( - _is_net_reachable_from_net) diff --git a/gbpservice/contrib/nfp/service_plugins/loadbalancer/__init__.py b/gbpservice/contrib/nfp/service_plugins/loadbalancer/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/service_plugins/loadbalancer/drivers/__init__.py b/gbpservice/contrib/nfp/service_plugins/loadbalancer/drivers/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/service_plugins/loadbalancer/drivers/nfp_lbaasv2_plugin_driver.py b/gbpservice/contrib/nfp/service_plugins/loadbalancer/drivers/nfp_lbaasv2_plugin_driver.py deleted file mode 100644 index 862166f92..000000000 --- a/gbpservice/contrib/nfp/service_plugins/loadbalancer/drivers/nfp_lbaasv2_plugin_driver.py +++ /dev/null @@ -1,27 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from gbpservice.contrib.nfp.config_orchestrator.common import topics -from gbpservice.contrib.nfp.configurator.drivers.loadbalancer.v2.\ - haproxy import haproxy_driver_constants -from neutron_lbaas.drivers.common import agent_driver_base as adb - - -class HaproxyOnVMPluginDriver(adb.AgentDriverBase): - device_driver = haproxy_driver_constants.DRIVER_NAME - - def __init__(self, plugin): - # Monkey patch LB agent topic and LB agent type - adb.lb_const.LOADBALANCER_AGENTV2 = topics.LBV2_NFP_CONFIGAGENT_TOPIC - adb.lb_const.AGENT_TYPE_LOADBALANCERV2 = 'NFP Loadbalancer V2 agent' - - super(HaproxyOnVMPluginDriver, self).__init__(plugin) diff --git a/gbpservice/contrib/nfp/service_plugins/vpn/__init__.py b/gbpservice/contrib/nfp/service_plugins/vpn/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/service_plugins/vpn/drivers/__init__.py b/gbpservice/contrib/nfp/service_plugins/vpn/drivers/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/service_plugins/vpn/drivers/nfp_vpnaas_driver.py b/gbpservice/contrib/nfp/service_plugins/vpn/drivers/nfp_vpnaas_driver.py deleted file mode 100644 index 704f9a190..000000000 --- a/gbpservice/contrib/nfp/service_plugins/vpn/drivers/nfp_vpnaas_driver.py +++ /dev/null @@ -1,288 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import socket -import time - -from neutron.db import agents_db -from neutron.db import agentschedulers_db -from neutron_lib import exceptions -from neutron_lib.plugins import directory -from neutron_lib import rpc as n_rpc -from neutron_vpnaas.db.vpn import vpn_validator -from neutron_vpnaas.services.vpn.plugin import VPNDriverPlugin -from neutron_vpnaas.services.vpn.plugin import VPNPlugin -from neutron_vpnaas.services.vpn.service_drivers import base_ipsec -import oslo_messaging - -from gbpservice._i18n import _ -from gbpservice.contrib.nfp.config_orchestrator.common import topics -from gbpservice.nfp.core import log as nfp_logging - -LOG = nfp_logging.getLogger(__name__) -BASE_VPN_VERSION = '1.0' -AGENT_TYPE_VPN = 'NFP Vpn agent' -ACTIVE = 'ACTIVE' -DOWN = 'DOWN' -ERROR = 'ERROR' -TIMEOUT = 80 - - -class VPNAgentHostingServiceNotFound(exceptions.NeutronException): - message = _("VPN Agent hosting vpn service '%(vpnservice_id)s' not found") - - -class VPNAgentNotFound(exceptions.NeutronException): - message = _("VPN Agent not found in agent_db") - - -class VPNPluginExt(VPNPlugin, agentschedulers_db.AgentSchedulerDbMixin): - """ - Extends the base VPN Plugin class to inherit agentdb too. - Required to get agent entry into the database. - """ - - def __init__(self): - super(VPNPluginExt, self).__init__() - - -class NFPIPsecVPNDriverCallBack(base_ipsec.IPsecVpnDriverCallBack): - """Callback for IPSecVpnDriver rpc.""" - - target = oslo_messaging.Target(version=BASE_VPN_VERSION) - - def __init__(self, driver): - super(NFPIPsecVPNDriverCallBack, self).__init__(driver) - self.driver = driver - - def update_status(self, context, status): - """Update status of vpnservices.""" - if 'ipsec_site_connections' not in status[0]: - status[0]['ipsec_site_connections'] = {} - plugin = self.driver.service_plugin - plugin.update_status_by_agent(context, status) - - -class NFPIPsecVpnAgentApi(base_ipsec.IPsecVpnAgentApi): - """API and handler for NFP IPSec plugin to agent RPC messaging.""" - target = oslo_messaging.Target(version=BASE_VPN_VERSION) - - def __init__(self, topic, default_version, driver): - super(NFPIPsecVpnAgentApi, self).__init__( - topic, default_version, driver) - - def _is_agent_hosting_vpnservice(self, agent): - """ - In case we have agent running on each compute node. - We have to write logic here to get - the agent which is hosting this vpn service - """ - host = agent['host'] - lhost = socket.gethostname() - if host == lhost: - return True - return False - - def _get_agent_hosting_vpnservice(self, admin_context, vpnservice_id): - filters = {'agent_type': [AGENT_TYPE_VPN]} - agents = directory.get_plugin().get_agents( - admin_context, filters=filters) - - try: - for agent in agents: - if not agent['alive']: - continue - res = self._is_agent_hosting_vpnservice(agent) - if res is True: - return agent - - # valid vpn agent is not found, hostname comparison might be - # failed. Return whichever agent is available. - for agent in agents: - if not agent['alive']: - LOG.debug("Cannot get a active vpn agent, skipped") - continue - return agent - except Exception: - raise VPNAgentNotFound() - - msg = ('No active vpn agent found. Configuration will fail.') - LOG.error(msg) - raise VPNAgentHostingServiceNotFound(vpnservice_id=vpnservice_id) - - def _agent_notification(self, context, method, vpnservice_id, - version=None, **kwargs): - admin_context = context.is_admin and context or context.elevated() - - if not version: - version = self.target.version - vpn_agent = self._get_agent_hosting_vpnservice( - admin_context, vpnservice_id) - - msg = (('Notify agent at %(topic)s.%(host)s the message ' - '%(method)s %(args)s') - % {'topic': self.topic, - 'host': vpn_agent['host'], - 'method': method, 'args': kwargs}) - LOG.debug(msg) - - cctxt = self.client.prepare(server=vpn_agent['host'], - version=version) - cctxt.cast(context, method, **kwargs) - - def vpnservice_updated(self, context, vpnservice_id, **kwargs): - """ - Make rpc to agent for 'vpnservice_updated' - """ - try: - self._agent_notification( - context, 'vpnservice_updated', - vpnservice_id, **kwargs) - except Exception: - msg = ('Notifying agent failed') - LOG.error(msg) - - -class VPNValidator(vpn_validator.VpnReferenceValidator): - """This class overrides the vpnservice validator method""" - def __init__(self): - super(VPNValidator, self).__init__() - - def validate_vpnservice(self, context, vpns): - pass - - -class NFPIPsecVPNDriver(base_ipsec.BaseIPsecVPNDriver): - """VPN Service Driver class for IPsec.""" - - def __init__(self, service_plugin): - super(NFPIPsecVPNDriver, self).__init__( - service_plugin) - self.validator = VPNValidator() - - def create_rpc_conn(self): - self.endpoints = [ - NFPIPsecVPNDriverCallBack(self), - agents_db.AgentExtRpcCallback(VPNPluginExt())] - - self.conn = n_rpc.create_connection(new=True) - self.conn.create_consumer( - topics.VPN_NFP_PLUGIN_TOPIC, self.endpoints, fanout=False) - self.conn.consume_in_threads() - self.agent_rpc = NFPIPsecVpnAgentApi( - topics.VPN_NFP_CONFIGAGENT_TOPIC, BASE_VPN_VERSION, self) - - def _get_service_vendor(self, context, vpnservice_id): - vpnservice = self.service_plugin.get_vpnservice( - context, vpnservice_id) - desc = vpnservice['description'] - # if the call is through GBP workflow, - # fetch the service profile from description - # else, use 'VYOS' as the service profile - if 'service_vendor=' in desc: - tokens = desc.split(';') - service_vendor = tokens[5].split('=')[1] - else: - service_vendor = 'VYOS' - return service_vendor - - def create_ipsec_site_connection(self, context, ipsec_site_connection): - service_vendor = self._get_service_vendor( - context, - ipsec_site_connection['vpnservice_id']) - - starttime = 0 - while starttime < TIMEOUT: - vpnservice = self.service_plugin.get_vpnservice( - context, - ipsec_site_connection['vpnservice_id']) - # (Revisit):Due to device driver issue neutron is making vpnservice - # state in Down state, At this point of time, - # Allowing ipsec site connection to gets created though - # vpnservice is in down state. - if vpnservice['status'] in [ACTIVE, DOWN]: - self.agent_rpc.vpnservice_updated( - context, - ipsec_site_connection['vpnservice_id'], - rsrc_type='ipsec_site_connection', - svc_type=self.service_type, - rsrc_id=ipsec_site_connection['id'], - resource=ipsec_site_connection, - reason='create', service_vendor=service_vendor) - break - elif vpnservice['status'] == ERROR: - msg = ('updating ipsec_site_connection with id %s to' - 'ERROR state' % (ipsec_site_connection['id'])) - LOG.error(msg) - VPNPluginExt().update_ipsec_site_conn_status( - context, - ipsec_site_connection['id'], - ERROR) - break - time.sleep(5) - starttime += 5 - else: - msg = ('updating ipsec_site_connection with id %s to' - 'ERROR state' % (ipsec_site_connection['id'])) - LOG.error(msg) - VPNPluginExt().update_ipsec_site_conn_status( - context, - ipsec_site_connection['id'], - ERROR) - - def _move_ipsec_conn_state_to_error(self, context, ipsec_site_connection): - vpnsvc_status = [{ - 'id': ipsec_site_connection['vpnservice_id'], - 'status':ERROR, - 'updated_pending_status':False, - 'ipsec_site_connections':{ - ipsec_site_connection['id']: { - 'status': ERROR, - 'updated_pending_status': True}}}] - driver = VPNDriverPlugin()._get_driver_for_ipsec_site_connection( - context, - ipsec_site_connection) - NFPIPsecVPNDriverCallBack(driver).update_status(context, - vpnsvc_status) - - def delete_ipsec_site_connection(self, context, ipsec_site_connection): - service_vendor = self._get_service_vendor( - context, - ipsec_site_connection['vpnservice_id']) - - self.agent_rpc.vpnservice_updated( - context, - ipsec_site_connection['vpnservice_id'], - rsrc_type='ipsec_site_connection', - svc_type=self.service_type, - rsrc_id=ipsec_site_connection['id'], - resource=ipsec_site_connection, - reason='delete', service_vendor=service_vendor) - - def create_vpnservice(self, context, vpnservice): - service_vendor = self._get_service_vendor(context, - vpnservice['id']) - - self.agent_rpc.vpnservice_updated( - context, - vpnservice['id'], - rsrc_type='vpn_service', - svc_type=self.service_type, - rsrc_id=vpnservice['id'], - resource=vpnservice, - reason='create', service_vendor=service_vendor) - - def delete_vpnservice(self, context, vpnservice): - pass - - def update_vpnservice(self, context, old_vpnservice, new_vpnservice): - pass diff --git a/gbpservice/contrib/nfp/tools/__init__.py b/gbpservice/contrib/nfp/tools/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp/tools/setup_nfp.py b/gbpservice/contrib/nfp/tools/setup_nfp.py deleted file mode 100755 index 118df26a3..000000000 --- a/gbpservice/contrib/nfp/tools/setup_nfp.py +++ /dev/null @@ -1,811 +0,0 @@ -#!/usr/bin/python - -import argparse -import sys -import os -import shutil -import subprocess -import configparser -import subprocess -import time -import platform -from .image_builder import disk_image_create as DIB - -# Defines -TEMP_WORK_DIR = "tmp" -CONFIG = configparser.ConfigParser() -NEUTRON_CONF = "/etc/neutron/neutron.conf" -NEUTRON_ML2_CONF = "/etc/neutron/plugins/ml2/ml2_conf.ini" -FILE_PATH = os.path.dirname(os.path.realpath(__file__)) -CONFIGURATOR_USER_DATA = FILE_PATH + "/image_builder/configurator_user_data" -TEMPLATES_PATH = FILE_PATH + "/templates/gbp_resources.yaml" -APIC_ENV = False - -# global values -# these src_dirs will be copied from host to inside docker image, these -# diretories are assumed to present in src_path -src_dirs = ["gbpservice", "neutron", "neutron_lbaas", "neutron_lib"] -# create a temp directory for copying srcs -dst_dir = "/tmp/controller_docker_build/" - - -parser = argparse.ArgumentParser() -parser.add_argument('--configure', action='store_true', - dest='configure_nfp', - default=False, help='Configure NFP') -parser.add_argument('--build-controller-vm', action='store_true', - dest='build_controller_vm', - default=False, help='enable building controller vm') -parser.add_argument('--image-build-cache-dir', type=str, - help=('directory path where trusty image tar.gz' - ' can be found for building controller vm')) -parser.add_argument('--enable-orchestrator', action='store_true', - dest='enable_orchestrator', - default=False, - help='enable creating orchestrator systemctl file') -parser.add_argument('--enable-proxy', action='store_true', - dest='enable_proxy', - default=False, - help='enable creating proxy systemctl file') -parser.add_argument('--create-resources', action='store_true', - dest='create_resources', - default=False, - help='enable creating nfp required resources') -parser.add_argument('--launch-controller', action='store_true', - dest='launch_controller', - default=False, help='enable to launch controller vm') -parser.add_argument('--configure-ext-net', - action='store_true', default=False, - help=('Configure heat driver section in nfp.ini.' - ' Specify external network name with --ext-net-name option.')) -parser.add_argument('--ext-net-name', type=str, - default='', - help=('Provide external network(neutron network) name.' - ' Use along with --configure-ext-net.')) -parser.add_argument('--clean-up', action='store_true', dest='clean_up_nfp', - default=False, - help='enable to clean up nfp services and resources') -parser.add_argument('--controller-path', type=str, dest='controller_path', - help='patch to the controller image') -args = parser.parse_args() - -def check_if_apic_sys(): - global APIC_ENV - mech_drivers = subprocess.getoutput("crudini --get " + NEUTRON_ML2_CONF + " ml2 mechanism_drivers") - if mech_drivers == 'apic_gbp': - APIC_ENV = True - -def set_keystone_authtoken_section(): - global NEUTRON_CONF - nfp_conf = '/etc/nfp.ini' - admin_user = subprocess.getoutput("crudini --get " + NEUTRON_CONF + " keystone_authtoken username") - admin_password = subprocess.getoutput("crudini --get " + NEUTRON_CONF + " keystone_authtoken password") - admin_tenant_name = subprocess.getoutput("crudini --get " + NEUTRON_CONF + " keystone_authtoken project_name") - auth_uri = subprocess.getoutput("crudini --get " + NEUTRON_CONF + " keystone_authtoken auth_uri") - auth_protocol = subprocess.getoutput("echo " + auth_uri + " | cut -d':' -f1") - auth_host = subprocess.getoutput("echo " + auth_uri + " | cut -d'/' -f3 | cut -d':' -f1") - auth_port = subprocess.getoutput("echo " + auth_uri + " | cut -d'/' -f3 | cut -d':' -f2") - auth_version = subprocess.getoutput("echo " + auth_uri + " | cut -d'/' -f4") - if auth_version == '': - auth_version = 'v2.0' - subprocess.call(("crudini --set " + nfp_conf + " nfp_keystone_authtoken admin_user " + admin_user).split(' ')) - subprocess.call(("crudini --set " + nfp_conf + " nfp_keystone_authtoken admin_password " + admin_password).split(' ')) - subprocess.call(("crudini --set " + nfp_conf + " nfp_keystone_authtoken admin_tenant_name " + admin_tenant_name).split(' ')) - subprocess.call(("crudini --set " + nfp_conf + " nfp_keystone_authtoken auth_protocol " + auth_protocol).split(' ')) - subprocess.call(("crudini --set " + nfp_conf + " nfp_keystone_authtoken auth_host " + auth_host).split(' ')) - subprocess.call(("crudini --set " + nfp_conf + " nfp_keystone_authtoken auth_port " + auth_port).split(' ')) - subprocess.call(("crudini --set " + nfp_conf + " nfp_keystone_authtoken auth_version " + auth_version).split(' ')) - -def configure_nfp(): - subprocess.getoutput("cat /usr/lib/python2.7/site-packages/gbpservice/contrib/nfp/bin/nfp.ini >> /etc/nfp.ini") - subprocess.getoutput("mkdir -p /etc/nfp/vyos/") - subprocess.getoutput("cp -r /usr/lib/python2.7/site-packages/gbpservice/contrib/nfp/bin/vyos.day0 /etc/nfp/vyos/") - subprocess.getoutput("sed -i 's/\"password\": \"\"/\"password\": \"vyos\"/' /etc/nfp/vyos/vyos.day0") - set_keystone_authtoken_section() - check_if_apic_sys() - curr_service_plugins = subprocess.getoutput("crudini --get /etc/neutron/neutron.conf DEFAULT service_plugins") - curr_service_plugins_list = curr_service_plugins.split(",") - lbaas_enabled = [x for x in curr_service_plugins_list if 'lbaas' in x] - vpnaas_enabled = [x for x in curr_service_plugins_list if 'vpnaas' in x] - fwaas_enabled = [x for x in curr_service_plugins_list if 'fwaas' in x] - firewall_enabled = [x for x in curr_service_plugins_list if 'firewall' in x] - for word in firewall_enabled: - if word not in fwaas_enabled: - fwaas_enabled.append(word) - plugins_to_enable = ["ncp"] - for plugin in plugins_to_enable: - if plugin not in curr_service_plugins_list: - curr_service_plugins_list.append(plugin) - - if "servicechain" in curr_service_plugins_list: - curr_service_plugins_list.remove("servicechain") - - if not len(vpnaas_enabled): - curr_service_plugins_list.append("vpnaas") - else: - for word in vpnaas_enabled: - curr_service_plugins_list.remove(word) - curr_service_plugins_list.append("vpnaas") - - # enable lbaasv2 by default - if not len(lbaas_enabled): - curr_service_plugins_list.append("lbaasv2") - else: - for word in lbaas_enabled: - curr_service_plugins_list.remove(word) - curr_service_plugins_list.append("lbaasv2") - - if not len(fwaas_enabled): - curr_service_plugins_list.append("nfp_fwaas") - else: - for word in fwaas_enabled: - curr_service_plugins_list.remove(word) - curr_service_plugins_list.append("nfp_fwaas") - - new_service_plugins_list = curr_service_plugins_list - new_service_plugins = ",".join(new_service_plugins_list) - subprocess.call(("crudini --set /etc/neutron/neutron.conf DEFAULT service_plugins " + str(new_service_plugins)).split(' ')) - - #check id gbp-heat is configured, if not configure - curr_heat_plugin_dirs = subprocess.getoutput("crudini --get /etc/heat/heat.conf DEFAULT plugin_dirs") - curr_heat_plugin_dirs_list = curr_heat_plugin_dirs.split(",") - heat_dirs_to_enable = ["/usr/lib64/heat", "/usr/lib/heat", "/usr/lib/python2.7/site-packages/gbpautomation/heat"] - for dir in heat_dirs_to_enable: - if dir not in curr_heat_plugin_dirs_list: - curr_heat_plugin_dirs_list.append(dir) - new_heat_plugin_dirs_list = curr_heat_plugin_dirs_list - new_heat_plugin_dirs = ",".join(new_heat_plugin_dirs_list) - subprocess.call(("crudini --set /etc/heat/heat.conf DEFAULT plugin_dirs " + str(new_heat_plugin_dirs)).split(' ')) - - # Enable GBP extension driver for service sharing - if not APIC_ENV: - subprocess.call("crudini --set /etc/neutron/neutron.conf group_policy policy_drivers implicit_policy,resource_mapping,chain_mapping".split(' ')) - else: - subprocess.call("crudini --set /etc/neutron/neutron.conf group_policy policy_drivers implicit_policy,apic,chain_mapping".split(' ')) - # Configure policy_drivers if section group_policy exists in the config file - ret = subprocess.call("crudini --get /etc/neutron/plugins/ml2/ml2_conf_cisco_apic.ini group_policy".split(' ')) - if not ret: - subprocess.call("crudini --set /etc/neutron/plugins/ml2/ml2_conf_cisco_apic.ini group_policy policy_drivers implicit_policy,apic,chain_mapping".split(' ')) - - subprocess.call("crudini --set /etc/neutron/neutron.conf group_policy extension_drivers proxy_group".split(' ')) - - # Configure service owner - subprocess.call("crudini --set /etc/neutron/neutron.conf admin_owned_resources_apic_tscp plumbing_resource_owner_user neutron".split(' ')) - admin_password = subprocess.getoutput("crudini --get /etc/neutron/neutron.conf keystone_authtoken password") - subprocess.call("crudini --set /etc/neutron/neutron.conf admin_owned_resources_apic_tscp plumbing_resource_owner_password".split(' ') + [admin_password]) - subprocess.call("crudini --set /etc/neutron/neutron.conf admin_owned_resources_apic_tscp plumbing_resource_owner_tenant_name services".split(' ')) - - # Configure NFP drivers - subprocess.call("crudini --set /etc/neutron/neutron.conf node_composition_plugin node_plumber admin_owned_resources_apic_plumber".split(' ')) - subprocess.call("crudini --set /etc/neutron/neutron.conf node_composition_plugin node_drivers nfp_node_driver".split(' ')) - subprocess.call("crudini --set /etc/neutron/neutron.conf nfp_node_driver is_service_admin_owned False".split(' ')) - subprocess.call("crudini --set /etc/neutron/neutron.conf nfp_node_driver svc_management_ptg_name svc_management_ptg".split(' ')) - - # Enable ML2 port security - subprocess.call("crudini --set /etc/neutron/plugins/ml2/ml2_conf.ini ml2 extension_drivers port_security".split(' ')) - - # Update neutron server to use GBP policy - subprocess.call("crudini --set /etc/neutron/neutron.conf DEFAULT policy_file /etc/group-based-policy/policy.d/policy.json".split(' ')) - - # Update neutron LBaaS with NFP LBaaS v2 service provider - subprocess.call("crudini --set /etc/neutron/neutron_lbaas.conf service_providers service_provider LOADBALANCERV2:loadbalancerv2:gbpservice.contrib.nfp.service_plugins.loadbalancer.drivers.nfp_lbaasv2_plugin_driver.HaproxyOnVMPluginDriver:default".split(' ')) - - # Update neutron VPNaaS with NFP VPNaaS service provider - subprocess.call(["grep -q '^service_provider.*NFPIPsecVPNDriver:default' /etc/neutron/neutron_vpnaas.conf; if [[ $? = 1 ]]; then sed -i '/^service_provider.*IPsecVPNDriver/ s/:default/\\nservice_provider\ =\ VPN:vpn:gbpservice.contrib.nfp.service_plugins.vpn.drivers.nfp_vpnaas_driver.NFPIPsecVPNDriver:default/' /etc/neutron/neutron_vpnaas.conf; fi"], shell=True) - - # Update DB - subprocess.call("gbp-db-manage --config-file /usr/share/neutron/neutron-dist.conf --config-file /etc/neutron/neutron.conf --config-file /etc/neutron/plugin.ini upgrade head".split(' ')) - - # Restart the services to make the configuration effective - subprocess.call("systemctl restart nfp_orchestrator".split(' ')) - subprocess.call("systemctl restart nfp_config_orch".split(' ')) - subprocess.call("systemctl restart openstack-heat-engine".split(' ')) - subprocess.call("systemctl restart neutron-server".split(' ')) - - -def get_src_dirs(): - print("Getting source dirs for copying inside the docker image") - # get the operating system type - (os_type, os_version, os_release) = platform.dist() - if os_type in ['Ubuntu']: - src_path = "/usr/lib/python2.7/dist-packages/" - elif os_type in ['centos', 'redhat']: - src_path = "/usr/lib/python2.7/site-packages/" - else: - print(("ERROR: Unsupported Operating System(%s)" % os_type)) - return 1 - for src_dir in src_dirs: - to_copy = src_path + src_dir - if not os.path.isdir(to_copy): - print(("ERROR: directory not found: ", to_copy)) - return 1 - # create a tmp directory for creating configurator docker - subprocess.call(["rm", "-rf", dst_dir]) - os.mkdir(dst_dir) - dockerfile = DIB.cur_dir + "/Dockerfile" - run_sh = DIB.cur_dir + "/configurator_run.sh" - # these src_dirs will be copied from host to inside docker image - for src_dir in src_dirs: - to_copy = src_path + src_dir - if(subprocess.call(["cp", "-r", to_copy, dst_dir])): - print(("ERROR: failed to copy %s to ./ directory" % to_copy)) - return 1 - subprocess.call(["cp", dockerfile, dst_dir]) - subprocess.call(["cp", run_sh, dst_dir]) - DIB.docker_build_dir = dst_dir - - return 0 - - -def clean_src_dirs(): - subprocess.call(["rm", "-rf", dst_dir]) - - -def build_configuration_vm(): - - cur_dir = os.path.dirname(__file__) - cur_dir = os.path.realpath(cur_dir) - if not cur_dir: - # if script is executed from current dir, get abs path - cur_dir = os.path.realpath('./') - # update dib current working dir - DIB.cur_dir = cur_dir + '/image_builder' - - if(get_src_dirs()): - return - - # set the cache dir where trusty tar.gz will be present - if args.image_build_cache_dir: - cache_dir = args.image_build_cache_dir - else: - cache_dir = os.environ.get('HOME', '-1') + '/.cache/image-create' - - # create a configurattion dictionary needed by DIB - DIB.conf['ubuntu_release'] = {'release': 'trusty'} - DIB.conf['dib'] = {"image_size": 10, "elements": ["configurator", "root-passwd"], - "root_pswd": "nfp123", - "offline": True, "cache_dir": cache_dir} - - # Build configurator VM - (ret, image) = DIB.dib() - if not ret: - print("ERROR: Failed to create Configurator VM") - else: - print(("SUCCESS, created Configurator VM: ", image)) - - # clean the scr_dirs copied in PWD - clean_src_dirs() - - os.chdir(cur_dir) - - return - - -def restart_nfp_orchestrator(): - try: - subprocess.call(["systemctl", "daemon-reload"]) - subprocess.call(["service", "nfp_orchestrator", "restart"]) - except Exception as error: - print("Error restarting nfp_orchestrator service") - print(error) - sys.exit(1) - - -def restart_nfp_config_orch(): - try: - subprocess.call(["systemctl", "daemon-reload"]) - subprocess.call(["service", "nfp_config_orch", "restart"]) - except Exception as error: - print("Error restarting nfp_orchestrator service") - print(error) - sys.exit(1) - - -def restart_nfp_proxy(): - try: - subprocess.call(["systemctl", "daemon-reload"]) - subprocess.call(["service", "nfp_proxy", "restart"]) - except Exception as error: - print("Error restarting nfp_proxy service") - print(error) - sys.exit(1) - - -def restart_nfp_proxy_agent(): - try: - subprocess.call(["systemctl", "daemon-reload"]) - subprocess.call(["service", "nfp_proxy_agent", "restart"]) - except Exception as error: - print("Error restarting nfp_proxy_agent service") - print(error) - sys.exit(1) - - -def create_orchestrator_ctl(): - """ - create nfp orchestrator systemctl service file - """ - - if not os.path.exists("/var/log/nfp"): - os.makedirs("/var/log/nfp") - os.system("chown neutron:neutron /var/log/nfp") - - if not os.path.exists(TEMP_WORK_DIR): - os.makedirs(TEMP_WORK_DIR) - - orch_ctl_file = TEMP_WORK_DIR + "/nfp_orchestrator.service" - try: - file = open(orch_ctl_file, 'w+') - except: - print(("Error creating " + orch_ctl_file + " file")) - sys.exit(1) - - file.write("[Unit]\nDescription=One Convergence NFP Orchestrator\n") - file.write("After=syslog.target network.target\n\n[Service]") - file.write("\nUser=neutron\nExecStart=/usr/bin/nfp --module orchestrator") - file.write(" --config-file /etc/neutron/neutron.conf --config-file ") - file.write(" /etc/neutron/plugins/ml2/ml2_conf.ini ") - file.write(" --config-file /etc/nfp.ini ") - file.write("--log-file /var/log/nfp/nfp_orchestrator.log\n\n") - file.write("[Install]\nWantedBy=multi-user.target") - file.close() - - if os.path.exists("/usr/lib/systemd/system"): - shutil.copy(orch_ctl_file, "/usr/lib/systemd/system/") - else: - print("Error: /usr/lib/systemd/system not present") - sys.exit(1) - - subprocess.call(["systemctl", "enable", "nfp_orchestrator"]) - - orch_config_file = TEMP_WORK_DIR + "/nfp_config_orch.service" - try: - file = open(orch_config_file, 'w+') - except: - print(("Error creating " + orch_ctl_file + " file")) - sys.exit(1) - - file.write("[Unit]\nDescription=One Convergence NFP Config Orchestrator") - file.write("\nAfter=syslog.target network.target") - file.write("\n\n[Service]\nType=simple\nUser=neutron") - file.write("\nExecStart=/usr/bin/nfp" - " --module config_orchestrator" - " --config-file /etc/nfp.ini") - file.write(" --config-file /etc/neutron/neutron.conf" - " --log-file /var/log/nfp/nfp_config_orch.log") - file.write("\n\n[Install]\nWantedBy=multi-user.target") - file.close() - - if os.path.exists("/usr/lib/systemd/system"): - shutil.copy(orch_config_file, "/usr/lib/systemd/system/") - else: - print("Error: /usr/lib/systemd/system not present") - sys.exit(1) - - subprocess.call(["systemctl", "enable", "nfp_config_orch"]) - - try: - shutil.rmtree(TEMP_WORK_DIR) - except: - print("Error: Cleaning up the temp directory") - sys.exit(1) - - -def create_nfp_namespace_file(): - """ - create nfp proxy systemctl service file - """ - if not os.path.exists(TEMP_WORK_DIR): - os.makedirs(TEMP_WORK_DIR) - - proxy_tool_file = TEMP_WORK_DIR + "/nfp_namespace" - try: - filepx = open(proxy_tool_file, 'w+') - except: - print(("Error creating " + proxy_tool_file + " file")) - sys.exit(1) - filepx.write("#!/usr/bin/bash\n") - filepx.write("\nNOVA_CONF=/etc/nova/nova.conf\nNOVA_SESSION=neutron") - filepx.write("\n\nget_openstack_creds () {") - filepx.write("\n\tAUTH_URI=`crudini --get $NOVA_CONF $NOVA_SESSION auth_url`") - filepx.write("\n\t# if auth_url option is not available, look for admin_auth_url" - "\n\tif [[ $? = 1 ]]; then" - "\n\t\tAUTH_URI=`crudini --get $NOVA_CONF $NOVA_SESSION admin_auth_url`" - "\n\tfi") - filepx.write("\n\tADMIN_USER=`crudini --get $NOVA_CONF $NOVA_SESSION username`") - filepx.write("\n\t# if username option is not available, look for admin_username" - "\n\tif [[ $? = 1 ]]; then" - "\n\t\tADMIN_USER=`crudini --get $NOVA_CONF $NOVA_SESSION admin_username`") - filepx.write("\n\t\t# if admin_username option is not available, look for admin_user" - "\n\t\tif [[ $? = 1 ]]; then" - "\n\t\t\tADMIN_USER=`crudini --get $NOVA_CONF $NOVA_SESSION admin_user`" - "\n\t\tfi" - "\n\tfi") - filepx.write("\n\tADMIN_PASSWD=`crudini --get $NOVA_CONF $NOVA_SESSION password`") - filepx.write("\n\t# if password option is not available, look for admin_password" - "\n\tif [[ $? = 1 ]]; then" - "\n\t\tADMIN_PASSWD=`crudini --get $NOVA_CONF $NOVA_SESSION admin_password`" - "\n\tfi") - filepx.write("\n\tADMIN_TENANT_NAME=`crudini --get $NOVA_CONF $NOVA_SESSION project_name`") - filepx.write("\n\t# if project_name option is not available, look for admin_tenant_name" - "\n\tif [[ $? = 1 ]]; then" - "\n\t\tADMIN_TENANT_NAME=`crudini --get $NOVA_CONF $NOVA_SESSION admin_tenant_name`" - "\n\tfi") - filepx.write("\n\texport OS_USERNAME=$ADMIN_USER") - filepx.write("\n\texport OS_TENANT_NAME=$ADMIN_TENANT_NAME") - filepx.write("\n\texport OS_PASSWORD=$ADMIN_PASSWD") - filepx.write("\n\tif [[ $AUTH_URI == *\"v3\"* ]]; then" - "\n\t\tADMIN_PROJECT_DOMAIN_NAME=`crudini --get $NOVA_CONF" - " $NOVA_SESSION project_domain_name`" - "\n\t\tADMIN_USER_DOMAIN_NAME=`crudini --get $NOVA_CONF" - " $NOVA_SESSION user_domain_name`" - "\n\t\texport OS_PROJECT_DOMAIN_NAME=$ADMIN_PROJECT_DOMAIN_NAME" - "\n\t\texport OS_USER_DOMAIN_NAME=$ADMIN_USER_DOMAIN_NAME" - "\n\tfi") - filepx.write("\n\texport OS_AUTH_URL=$AUTH_URI\n\n}") - filepx.write("\n\nfunction namespace_delete {\n\tget_openstack_creds") - filepx.write("\n\n\tproxyPortId=`neutron port-list | ") - filepx.write("grep pt_nfp_proxy_pt | awk '{print $2}'`") - filepx.write("\n\ttapName=\"tap${proxyPortId:0:11}\"\n\n" - "\t#Deletion namespace") - filepx.write("\n\tNFP_P=`ip netns | grep \"nfp-proxy\"`") - filepx.write("\n\tif [ ${#NFP_P} -ne 0 ]; then\n\t\t" - "ip netns delete nfp-proxy") - filepx.write("\n\t\techo \"namespace removed\"\n\tfi") - filepx.write("\n\n\t#pt1 port removing from ovs") - filepx.write("\n\tPORT=`ovs-vsctl show | grep \"$tapName\"`") - filepx.write("\n\tif [ ${#PORT} -ne 0 ]; then") - filepx.write("\n\t\tovs-vsctl del-port br-int $tapName") - filepx.write("\n\t\techo \"ovs port is removed\"") - filepx.write("\n\tfi\n\tpkill nfp_proxy") - filepx.write("\n\n\tgbp pt-delete nfp_proxy_pt") - filepx.write("\n\n\techo \"nfp-proxy cleaning success.... \"\n\n}") - filepx.write("\n\nfunction netmask_to_bitmask {") - filepx.write("\n\tnetmask_bits=$1") - filepx.write("\n\tset -- $(( 5 - ($netmask_bits / 8) )) 255 255 255 255 $(( (255 << (8 - ($netmask_bits % 8))) & 255 )) 0 0 0") - filepx.write("\n\t[ $1 -gt 1 ] && shift $1 || shift") - filepx.write("\n\tnetmask=${1-0}.${2-0}.${3-0}.${4-0}\n}") - filepx.write("\n\nfunction namespace_create {\n\n\tget_openstack_creds") - filepx.write("\n\tSERVICE_MGMT_GROUP=\"svc_management_ptg\"") - filepx.write("\n\tnetmask_bits=`neutron net-list --name l2p_$SERVICE_MGMT_GROUP -F subnets -f value | awk '{print $2}' | awk -F'/' '{print $2}'`") - filepx.write("\n\techo \"Creating new namespace nfp-proxy....\"") - filepx.write("\n\n\t#new namespace with name proxy") - filepx.write("\n\tNFP_P=`ip netns add nfp-proxy`") - filepx.write("\n\tif [ ${#NFP_P} -eq 0 ]; then") - filepx.write("\n\t\techo \"New namepace nfp-proxy create\"") - filepx.write("\n\telse\n\t\techo \"nfp-proxy creation failed\"\n\t\t" - "exit 0") - filepx.write("\n\tfi\n\n\t# create nfp_proxy pt") - filepx.write("\n\tgbp pt-create --policy-target-group $SERVICE_MGMT_GROUP" - " nfp_proxy_pt") - filepx.write("\n\n\t# Get the nfp_proxy_pt port id, mac address") - filepx.write("\n\tproxyPortId=`neutron port-list | grep pt_nfp_proxy_pt" - " | awk '{print $2}'`") - filepx.write("\n\tproxyMacAddr=`neutron port-list | grep pt_nfp_proxy_pt" - " | awk '{print $6}'`") - filepx.write("\n\tproxyPortIp=`neutron port-list | grep pt_nfp_proxy_pt" - " | awk '{print $11}' | sed 's/^\"\(.*\)\"}$/\\1/'`") - filepx.write("\n\ttapName=\"tap${proxyPortId:0:11}\"") - filepx.write("\n\tnew_ip_cidr=\"$proxyPortIp/$netmask_bits\"") - filepx.write("\n\tnetmask_to_bitmask $netmask_bits\n") - filepx.write("\n\tproxyBrd=`ipcalc -4 $proxyPortIp -m $netmask -b" - " | grep BROADCAST | awk -F '=' '{print $2}'`") - filepx.write("\n\n\t# Create a tap interface and add it" - " to the ovs bridge br-int") - filepx.write("\n\tovs-vsctl add-port br-int $tapName -- set Interface" - " $tapName type=internal") - filepx.write(" external_ids:iface-id=$proxyPortId" - " external_ids:iface-status=active" - " external_ids:attached-mac=$proxyMacAddr") - filepx.write("\n\n\t# Add the tap interface to proxy\n\t" - "ip link set $tapName netns nfp-proxy") - filepx.write("\n\n\t# Get the link up\n\tip netns exec nfp-proxy" - " ip link set $tapName up") - filepx.write("\n\n\t# set the mac address on the tap interface\n\t" - "ip netns exec nfp-proxy" - " ip link set $tapName address $proxyMacAddr") - filepx.write("\n\n\t# assign ip address to the proxy tap interface") - filepx.write("\n\tip netns exec nfp-proxy ip -4 addr add" - " $new_ip_cidr scope global dev $tapName brd $proxyBrd") - filepx.write("\n\n\t# Update the neutron port with the host id binding") - filepx.write("\n\tneutron port-update $proxyPortId" - " --binding:host_id=`hostname`") - filepx.write("\n\n\tPING=`ip netns exec nfp-proxy" - " ping $1 -q -c 2 > /dev/null`") - filepx.write("\n\tif [ ${#PING} -eq 0 ]\n\tthen") - filepx.write("\n\t\techo \"nfp-proxy namespcace creation success and" - " reaching to $1\"") - filepx.write("\n\telse\n\t\techo \"Fails reaching to $1\"") - filepx.write("\n\tfi\n\n\tip netns exec nfp-proxy /usr/bin/nfp_proxy") - filepx.write(" --config-file=$2" - " --log-file /var/log/nfp/nfp_proxy.log") - filepx.write("\n}") - filepx.close() - - if os.path.exists("/usr/lib/python2.7/site-packages/gbpservice/nfp/" - "tools/"): - shutil.copy(proxy_tool_file, - "/usr/lib/python2.7/site-packages/gbpservice/nfp/tools/") - else: - os.makedirs("/usr/lib/python2.7/site-packages/gbpservice/nfp/tools") - shutil.copy(proxy_tool_file, "/usr/lib/python2.7/site-packages/gbpservice/nfp/tools/") - - try: - shutil.rmtree(TEMP_WORK_DIR) - except: - print("Error: Cleaning up the temp directory") - sys.exit(1) - - -def create_proxy_ctl(): - """ - create nfp proxy systemctl service file - """ - - if not os.path.exists("/var/log/nfp"): - os.makedirs("/var/log/nfp") - - if not os.path.exists(TEMP_WORK_DIR): - os.makedirs(TEMP_WORK_DIR) - - proxy_sup_file = TEMP_WORK_DIR + "/nfpproxy_startup" - try: - filepx = open(proxy_sup_file, 'w+') - except: - print(("Error creating " + proxy_sup_file + " file")) - sys.exit(1) - - filepx.write("#!/usr/bin/sh\nNFP_PROXY_AGENT_INI=/etc/nfp.ini") - filepx.write("\nCONFIGURATOR_IP=`crudini --get $NFP_PROXY_AGENT_INI" - " PROXY nfp_controller_ip`\n") - filepx.write(". /usr/lib/python2.7/site-packages/gbpservice/nfp/tools/" - "nfp_namespace;") - filepx.write("namespace_delete ;namespace_create $CONFIGURATOR_IP $NFP_PROXY_AGENT_INI") - filepx.close() - - proxy_ctl_file = TEMP_WORK_DIR + "/nfp_proxy.service" - try: - file = open(proxy_ctl_file, 'w+') - except: - print(("Error creating " + proxy_ctl_file + " file")) - sys.exit(1) - - file.write("[Unit]\nDescription=One Convergence NFP Proxy\n") - file.write("After=syslog.target network.target\n\n") - file.write("\n[Service]\nUser=root\nExecStart=/usr/bin/nfpproxy_startup") - file.write("\nRestart=on-abort") - file.write("\n\n[Install]\nWantedBy=multi-user.target") - file.close() - - if os.path.exists("/usr/lib/systemd/system"): - shutil.copy(proxy_ctl_file, "/usr/lib/systemd/system/") - else: - print("error: /usr/lib/systemd/system not present") - sys.exit(1) - - if os.path.exists("/usr/bin"): - shutil.copy(proxy_sup_file, "/usr/bin/") - os.system("chmod +x /usr/bin/nfpproxy_startup") - else: - print("error: /usr/bin not present") - sys.exit(1) - - subprocess.call(["systemctl", "enable", "nfp_proxy"]) - - try: - shutil.rmtree(TEMP_WORK_DIR) - except: - print("Error: Cleaning up the temp directory") - sys.exit(1) - - -def create_proxy_agent_ctl(): - """ - create nfp proxy agent systemctl service file - """ - if not os.path.exists(TEMP_WORK_DIR): - os.makedirs(TEMP_WORK_DIR) - - proxy_ctl_file = TEMP_WORK_DIR + "/nfp_proxy_agent.service" - try: - file = open(proxy_ctl_file, 'w+') - except: - print(("Error creating " + proxy_ctl_file + " file")) - sys.exit(1) - - file.write("[Unit]\nDescription=One Convergence NFP Proxy Agent") - file.write("\nAfter=syslog.target network.target\n") - file.write("\n[Service]\nUser=root") - file.write("\nExecStart=/usr/bin/nfp --module proxy_agent " - "--config-file /etc/neutron/neutron.conf ") - file.write("--config-file /etc/nfp.ini ") - file.write("--log-file /var/log/nfp/nfp_proxy_agent.log\n") - file.write("\n[Install]\nWantedBy=multi-user.target\n") - file.close() - - if os.path.exists("/usr/lib/systemd/system"): - shutil.copy(proxy_ctl_file, "/usr/lib/systemd/system/") - else: - print("error: /usr/lib/systemd/system not present") - sys.exit(1) - - subprocess.call(["systemctl", "enable", "nfp_proxy_agent"]) - - try: - shutil.rmtree(TEMP_WORK_DIR) - except: - print("Error: Cleaning up the temp directory") - sys.exit(1) - - -def get_openstack_creds(): - CONFIG.read(NEUTRON_CONF) - AUTH_URI = CONFIG.get('keystone_authtoken', 'auth_uri') - AUTH_USER = CONFIG.get('keystone_authtoken', 'username') - AUTH_PASSWORD = CONFIG.get('keystone_authtoken', 'password') - AUTH_TENANT_NAME = CONFIG.get('keystone_authtoken', 'project_name') - os.environ["OS_USERNAME"] = AUTH_USER - os.environ["OS_TENANT_NAME"] = AUTH_TENANT_NAME - os.environ["OS_PASSWORD"] = AUTH_PASSWORD - os.environ["OS_AUTH_URL"] = AUTH_URI - - -def create_nfp_resources(): - """ - create nfp resources - """ - get_openstack_creds() - os.system("gbp l3policy-create default-nfp --ip-pool 172.16.0.0/16" - " --subnet-prefix-length 20 --proxy-ip-pool=172.17.0.0/16") - l3policy_Id = subprocess.getstatusoutput( - "gbp l3policy-list | grep '\sdefault-nfp\s' | awk '{print $2}'")[1] - os.system("gbp l2policy-create --l3-policy " + - l3policy_Id + " svc_management_ptg") - l2policy_Id = subprocess.getstatusoutput( - "gbp l2policy-list | grep '\ssvc_management_ptg\s'" - " | awk '{print $2}'")[1] - os.system("gbp group-create svc_management_ptg --service_management True" - " --l2-policy " + l2policy_Id) - - # Create GBP Resources Heat stack - os.system("heat stack-create --poll --template-file " + TEMPLATES_PATH + - " gbp_services_stack") - - -def add_nova_key_pair(): - tools_dir = os.path.dirname(__file__) - tools_dir = os.path.realpath(tools_dir) - if not tools_dir: - # if script is executed from current dir, get abs path - tools_dir = os.path.realpath('./') - os.chdir(tools_dir) - subprocess.call(["mkdir", "-p", "keys"]) - - configurator_key_name = "configurator_key" - print("Creating nova keypair for configurator VM.") - pem_file_content = subprocess.getoutput("nova keypair-add" + " " + configurator_key_name) - with open("keys/configurator_key.pem", "w") as f: - f.write(pem_file_content) - os.chmod("keys/configurator_key.pem", 0o600) - return configurator_key_name - - -def launch_configurator(): - get_openstack_creds() - if os.path.isfile(args.controller_path): - os.system("glance image-create --name nfp_controller" - " --disk-format qcow2 --container-format bare" - " --visibility public --file " + args.controller_path) - else: - print(("Error " + args.controller_path + " does not exist")) - sys.exit(1) - - # add nova keypair for nfp_controller VM. - configurator_key_name = add_nova_key_pair() - - Port_id = subprocess.getstatusoutput( - "gbp policy-target-create --policy-target-group svc_management_ptg" - " nfp_controllerVM_instance | grep port_id | awk '{print $4}'")[1] - Image_id = subprocess.getstatusoutput( - "glance image-list | grep nfp_controller |awk '{print $2}'")[1] - if Image_id and Port_id: - os.system("nova boot --flavor m1.medium --image " + - Image_id + " --user-data " + CONFIGURATOR_USER_DATA + - " --key-name " + configurator_key_name + - " --nic port-id=" + Port_id + " nfp_controllerVM_instance") - else: - if not Port_id: - print("Error unable to create the controller port id") - else: - print("Error unable to get nfp_controller image info") - sys.exit(1) - - -def configure_ext_net(ext_net_name): - os.system("crudini --set /etc/nfp.ini heat_driver" - " internet_out_network_name %s" - % (ext_net_name)) - subprocess.call("systemctl restart nfp_orchestrator".split(' ')) - - -def clean_up(): - """ - clean up nfp resources - """ - get_openstack_creds() - InstanceId = subprocess.getstatusoutput( - "nova list | grep nfp_controllerVM_instance | awk '{print $2}'")[1] - if InstanceId: - os.system("nova delete " + InstanceId) - time.sleep(10) - - PolicyTargetId = subprocess.getstatusoutput( - "gbp policy-target-list | grep nfp_controllerVM_instance" - " | awk '{print $2}'")[1] - if PolicyTargetId: - os.system("gbp policy-target-delete " + PolicyTargetId) - - ImageId = subprocess.getstatusoutput( - "glance image-list | grep nfp_controller | awk '{print $2}'")[1] - if ImageId: - os.system("glance image-delete " + ImageId) - - ServiceMGMTId = subprocess.getstatusoutput( - "gbp group-list | grep '\ssvc_management_ptg\s'" - " | awk '{print $2}'")[1] - if ServiceMGMTId: - SvcGroupId = subprocess.getstatusoutput( - "gbp group-list | grep '\ssvc_management_ptg\s'" - " | awk '{print $2}'")[1] - l2policyId = subprocess.getstatusoutput( - "gbp l2policy-list | grep '\ssvc_management_ptg\s'" - " | awk '{print $2}'")[1] - l3policyId = subprocess.getstatusoutput( - "gbp l3policy-list | grep '\sdefault-nfp\s'" - " | awk '{print $2}'")[1] - os.system("gbp group-delete " + SvcGroupId) - os.system("gbp l2policy-delete " + l2policyId) - os.system("gbp l3policy-delete " + l3policyId) - - HeatId = subprocess.getstatusoutput( - "heat stack-list | grep '\sgbp_services_stack\s'" - " | awk '{print $2}'")[1] - if HeatId: - os.system("heat stack-delete gbp_services_stack -y") - - -def main(): - if args.configure_nfp: - configure_nfp() - elif args.build_controller_vm: - build_configuration_vm() - elif args.enable_orchestrator: - create_orchestrator_ctl() - restart_nfp_orchestrator() - restart_nfp_config_orch() - elif args.enable_proxy: - create_nfp_namespace_file() - create_proxy_ctl() - restart_nfp_proxy() - create_proxy_agent_ctl() - restart_nfp_proxy_agent() - elif args.create_resources: - create_nfp_resources() - elif args.launch_controller: - if args.controller_path: - launch_configurator() - else: - parser.print_help() - elif args.configure_ext_net: - if args.ext_net_name != '': - configure_ext_net(args.ext_net_name) - else: - parser.print_help() - elif args.clean_up_nfp: - clean_up() - else: - parser.print_help() - - -if __name__ == '__main__': - main() diff --git a/gbpservice/contrib/nfp_service/__init__.py b/gbpservice/contrib/nfp_service/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp_service/reference_configurator/__init__.py b/gbpservice/contrib/nfp_service/reference_configurator/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/contrib/nfp_service/reference_configurator/controllers/controller.py b/gbpservice/contrib/nfp_service/reference_configurator/controllers/controller.py deleted file mode 100644 index c2eee95b8..000000000 --- a/gbpservice/contrib/nfp_service/reference_configurator/controllers/controller.py +++ /dev/null @@ -1,322 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import subprocess -import time - -import netaddr -import netifaces -from oslo_log import log as logging -import oslo_serialization.jsonutils as jsonutils -import pecan -from pecan import rest -import yaml - -from gbpservice._i18n import _ - -LOG = logging.getLogger(__name__) -SUCCESS = 'SUCCESS' -FAILED = 'FAILED' - -notifications = [] -FW_SCRIPT_PATH = ("/usr/local/lib/python2.7/dist-packages/" + - "gbpservice/contrib/nfp_service/" + - "reference_configurator/scripts/configure_fw_rules.py") - - -class Controller(rest.RestController): - - """Implements all the APIs Invoked by HTTP requests. - - Implements following HTTP methods. - -get - -post - - """ - def __init__(self, method_name): - try: - self.method_name = "network_function_device_notification" - super(Controller, self).__init__() - ip_a = subprocess.Popen('ifconfig -a', shell=True, - stdout=subprocess.PIPE).stdout.read() - out1 = subprocess.Popen('dhclient eth0', shell=True, - stdout=subprocess.PIPE).stdout.read() - out2 = subprocess.Popen('dhclient eth0', shell=True, - stdout=subprocess.PIPE).stdout.read() - output = "%s\n%s\n%s" % (ip_a, out1, out2) - LOG.info("Dhclient on eth0, result: %(output)s", - {'output': output}) - except Exception as err: - msg = ( - "Failed to initialize Controller class %s." % - str(err).capitalize()) - LOG.error(msg) - - def _push_notification(self, context, - notification_data, service_type): - response = {'info': {'service_type': service_type, - 'context': context}, - 'notification': notification_data - } - - notifications.append(response) - - @pecan.expose(method='GET', content_type='application/json') - def get(self): - """Method of REST server to handle request get_notifications. - - This method send an RPC call to configurator and returns Notification - data to config-agent - - Returns: Dictionary that contains Notification data - - """ - - global notifications - try: - notification_data = jsonutils.dumps(notifications) - msg = ("NOTIFICATION_DATA sent to config_agent %s" - % notification_data) - LOG.info(msg) - notifications = [] - return notification_data - except Exception as err: - pecan.response.status = 500 - msg = ("Failed to get notification_data %s." - % str(err).capitalize()) - LOG.error(msg) - error_data = self._format_description(msg) - return jsonutils.dumps(error_data) - - @pecan.expose(method='POST', content_type='application/json') - def post(self, **body): - try: - body = None - if pecan.request.is_body_readable: - body = pecan.request.json_body - - msg = ("Request data:: %s" % body) - LOG.debug(msg) - - config_datas = body['config'] - service_type = body['info']['service_type'] - notification_data = [] - - for config_data in config_datas: - try: - resource = config_data['resource'] - if resource == 'healthmonitor': - self._configure_healthmonitor(config_data) - elif resource == 'interfaces': - self._configure_interfaces(config_data) - elif resource == 'routes': - self._add_routes(config_data) - elif (config_data['resource'] in ['ansible', 'heat', - 'custom_json']): - self._apply_user_config(config_data) - else: - status_msg = 'Unsupported resource' - notification_data.append( - {'resource': resource, - 'data': {'status_code': FAILED, - 'status_msg': status_msg}}) - notification_data.append( - {'resource': config_data['resource'], - 'data': {'status_code': SUCCESS}}) - except Exception as ex: - notification_data.append( - {'resource': resource, - 'data': {'status_code': FAILED, - 'status_msg': str(ex)}}) - - context = body['info']['context'] - self._push_notification(context, notification_data, - service_type) - except Exception as err: - pecan.response.status = 500 - msg = ("Failed to serve HTTP post request %s %s." - % (self.method_name, str(err).capitalize())) - LOG.error(msg) - error_data = self._format_description(msg) - return jsonutils.dumps(error_data) - - def _format_description(self, msg): - """This method formats error description. - - :param msg: An error message that is to be formatted - - Returns: error_data dictionary - """ - - return {'failure_desc': {'msg': msg}} - - def _configure_healthmonitor(self, config_data): - LOG.info("Configures healthmonitor with configuration " - "data : %(healthmonitor_data)s ", - {'healthmonitor_data': config_data}) - - def _configure_interfaces(self, config_data): - out1 = subprocess.Popen('sudo dhclient eth1', shell=True, - stdout=subprocess.PIPE).stdout.read() - out2 = subprocess.Popen('sudo dhclient eth2', shell=True, - stdout=subprocess.PIPE).stdout.read() - out3 = subprocess.Popen('cat /etc/network/interfaces', shell=True, - stdout=subprocess.PIPE).stdout.read() - output = "%s\n%s\n%s" % (out1, out2, out3) - LOG.info("Dhclient on eth0, result: %(initial_data)s", - {'initial_data': output}) - LOG.info("Configures interfaces with configuration " - "data : %(interface_data)s ", - {'interface_data': config_data}) - - def get_source_cidrs_and_gateway_ip(self, route_info): - nfds = route_info['resource_data']['nfds'] - source_cidrs = [] - for nfd in nfds: - for network in nfd['networks']: - source_cidrs.append(network['cidr']) - if network['type'] == 'stitching': - gateway_ip = network['gw_ip'] - return source_cidrs, gateway_ip - - def _add_routes(self, route_info): - LOG.info("Configuring routes with configuration " - "data : %(route_data)s ", - {'route_data': route_info['resource_data']}) - source_cidrs, gateway_ip = self.get_source_cidrs_and_gateway_ip( - route_info) - default_route_commands = [] - for cidr in source_cidrs: - try: - source_interface = self._get_if_name_by_cidr(cidr) - except Exception: - raise Exception(_("Some of the interfaces do not have " - "IP Address")) - try: - interface_number_string = source_interface.split("eth", 1)[1] - except IndexError: - LOG.error("Retrieved wrong interface %(interface)s for " - "configuring routes", - {'interface': source_interface}) - try: - routing_table_number = 20 + int(interface_number_string) - - ip_rule_command = "ip rule add from %s table %s" % ( - cidr, routing_table_number) - out1 = subprocess.Popen(ip_rule_command, shell=True, - stdout=subprocess.PIPE).stdout.read() - ip_rule_command = "ip rule add to %s table main" % (cidr) - out2 = subprocess.Popen(ip_rule_command, shell=True, - stdout=subprocess.PIPE).stdout.read() - ip_route_command = "ip route add table %s default via %s" % ( - routing_table_number, gateway_ip) - default_route_commands.append(ip_route_command) - output = "%s\n%s" % (out1, out2) - LOG.info("Static route configuration result: %(output)s", - {'output': output}) - except Exception as ex: - raise Exception(_("Failed to add static routes: %(ex)s") % { - 'ex': str(ex)}) - for command in default_route_commands: - try: - out = subprocess.Popen(command, shell=True, - stdout=subprocess.PIPE).stdout.read() - LOG.info("Static route configuration result: %(output)s", - {'output': out}) - except Exception as ex: - raise Exception(_("Failed to add static routes: %(ex)s") % { - 'ex': str(ex)}) - - def _get_if_name_by_cidr(self, cidr): - interfaces = netifaces.interfaces() - retry_count = 0 - while True: - all_interfaces_have_ip = True - for interface in interfaces: - inet_list = netifaces.ifaddresses(interface).get( - netifaces.AF_INET) - if not inet_list: - all_interfaces_have_ip = False - for inet_info in inet_list or []: - netmask = inet_info.get('netmask') - ip_address = inet_info.get('addr') - subnet_prefix = cidr.split("/") - if (ip_address == subnet_prefix[0] and ( - len(subnet_prefix) == 1 or subnet_prefix[1] == "32")): - return interface - ip_address_netmask = '%s/%s' % (ip_address, netmask) - interface_cidr = netaddr.IPNetwork(ip_address_netmask) - if str(interface_cidr.cidr) == cidr: - return interface - # Sometimes the hotplugged interface takes time to get IP - if not all_interfaces_have_ip: - if retry_count < 10: - time.sleep(3) - retry_count = retry_count + 1 - continue - else: - raise Exception(_("Some of the interfaces do not have " - "IP Address")) - - def _apply_user_config(self, config_data): - LOG.info("Applying user config with configuration " - "type : %(config_type)s and " - "configuration data : %(config_data)s ", - {'config_type': config_data['resource'], - 'config_data': config_data['resource_data']}) - service_config = config_data['resource_data'][ - 'config_string'] - service_config = str(service_config) - if config_data['resource'] == 'ansible': - config_str = service_config.lstrip('ansible:') - rules = config_str - elif config_data['resource'] == 'heat': - config_str = service_config.lstrip('heat_config:') - rules = self._get_rules_from_config(config_str) - elif config_data['resource'] == 'custom_json': - config_str = service_config.lstrip('custom_json:') - rules = config_str - - fw_rule_file = FW_SCRIPT_PATH - command = ("sudo python " + fw_rule_file + " '" + - rules + "'") - subprocess.check_output(command, stderr=subprocess.STDOUT, - shell=True) - - def _get_rules_from_config(self, config_str): - rules_list = [] - try: - stack_template = (jsonutils.loads(config_str) if - config_str.startswith('{') else - yaml.load(config_str)) - except Exception: - return config_str - - resources = stack_template['resources'] - for resource in resources: - if resources[resource]['type'] == 'OS::Neutron::FirewallRule': - rule_info = {} - destination_port = '' - rule = resources[resource]['properties'] - protocol = rule['protocol'] - rule_info['action'] = 'log' - rule_info['name'] = protocol - if rule.get('destination_port'): - destination_port = rule['destination_port'] - if protocol == 'tcp': - rule_info['service'] = (protocol + '/' + - str(destination_port)) - else: - rule_info['service'] = protocol - rules_list.append(rule_info) - - return jsonutils.dumps({'rules': rules_list}) diff --git a/gbpservice/contrib/nfp_service/reference_configurator/scripts/configure_fw_rules.py b/gbpservice/contrib/nfp_service/reference_configurator/scripts/configure_fw_rules.py deleted file mode 100644 index df5c99a54..000000000 --- a/gbpservice/contrib/nfp_service/reference_configurator/scripts/configure_fw_rules.py +++ /dev/null @@ -1,104 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from subprocess import call -from subprocess import PIPE -from subprocess import Popen -import sys - -from oslo_log import log as logging -from oslo_serialization import jsonutils - -LOG = logging.getLogger(__name__) - - -class ConfigureIPtables(object): - - def __init__(self, json_blob): - ps = Popen(["sysctl", "net.ipv4.ip_forward"], stdout=PIPE) - output = ps.communicate()[0] - if "0" in output: - LOG.info("Enabling IP forwarding ...") - call(["sysctl", "-w", "net.ipv4.ip_forward=1"]) - else: - LOG.info("IP forwarding already enabled") - try: - self.rules_json = jsonutils.loads(json_blob) - except ValueError: - sys.exit('Given json_blob is not a valid json') - - def update_chain(self): - ps = Popen(["iptables", "-L"], stdout=PIPE) - output = ps.communicate()[0] - - # check if chain is present if not create new chain - if "testchain" not in output: - LOG.info("Creating new chain ...") - call(["iptables", "-F"]) - call(["iptables", "-N", "testchain"]) - call( - ["iptables", "-t", "filter", - "-A", "FORWARD", "-j", "testchain"]) - call(["iptables", "-A", "FORWARD", "-j", "DROP"]) - - # flush chain of existing rules - call(["iptables", "-F", "testchain"]) - # return - - # Update chain with new rules - LOG.info("Updating chain with new rules ...") - count = 0 - for rule in self.rules_json.get('rules'): - LOG.info("adding rule %(count)d", {'count': count}) - try: - action_values = ["LOG", "ACCEPT"] - action = rule['action'].upper() - if action not in action_values: - sys.exit( - "Action %s is not valid action! Please enter " - "valid action (LOG or ACCEPT)" % (action)) - service = rule['service'].split('/') - except KeyError as e: - sys.exit('KeyError: Rule does not have key %s' % (e)) - - if len(service) > 1: - ps = Popen(["iptables", "-A", "testchain", "-p", service[ - 0], "--dport", service[1], "-j", action], - stdout=PIPE) - else: - ps = Popen( - ["iptables", "-A", "testchain", "-p", service[0], - "-j", action], stdout=PIPE) - output = ps.communicate()[0] - if output: - LOG.error("Unable to add rule to chain due to: %(msg)s", - {'msg': output}) - count = count + 1 - ps = Popen(["iptables", "-A", "testchain", "-m", "state", "--state", - "ESTABLISHED,RELATED", "-j", "ACCEPT"], stdout=PIPE) - output = ps.communicate()[0] - if output: - LOG.error("Unable to add rule to chain due to: %(output)s", - {'output': output}) - - -def main(): - if len(sys.argv) < 2: - sys.exit('Usage: %s json-blob' % sys.argv[0]) - else: - json_blob = sys.argv[1] - test = ConfigureIPtables(json_blob) - test.update_chain() - - -if __name__ == "__main__": - main() diff --git a/gbpservice/neutron/services/servicechain/plugins/ncp/node_drivers/nfp_node_driver.py b/gbpservice/neutron/services/servicechain/plugins/ncp/node_drivers/nfp_node_driver.py index 6a958b3a5..2a7e8d9f6 100644 --- a/gbpservice/neutron/services/servicechain/plugins/ncp/node_drivers/nfp_node_driver.py +++ b/gbpservice/neutron/services/servicechain/plugins/ncp/node_drivers/nfp_node_driver.py @@ -11,37 +11,20 @@ # See the License for the specific language governing permissions and # limitations under the License. -import sys import threading -import eventlet from eventlet import greenpool -from keystoneclient import exceptions as k_exceptions -from keystoneclient.v2_0 import client as keyclient from neutron_lib.db import model_base from neutron_lib import exceptions as n_exc from neutron_lib.plugins import constants as pconst -from neutron_lib import rpc as n_rpc from oslo_config import cfg from oslo_log import log as logging -import oslo_messaging -from oslo_serialization import jsonutils -from oslo_utils import excutils -import six import sqlalchemy as sa from gbpservice._i18n import _ -from gbpservice.common import utils -from gbpservice.neutron.services.grouppolicy.common import constants as gconst from gbpservice.neutron.services.servicechain.plugins.ncp import ( exceptions as exc) -from gbpservice.neutron.services.servicechain.plugins.ncp import ( - model as ncp_model) -from gbpservice.neutron.services.servicechain.plugins.ncp import driver_base -from gbpservice.neutron.services.servicechain.plugins.ncp import plumber_base from gbpservice.nfp.common import constants as nfp_constants -from gbpservice.nfp.common import topics as nfp_rpc_topics -from gbpservice.nfp.orchestrator.db import nfp_db as nfp_db NFP_NODE_DRIVER_OPTS = [ @@ -150,119 +133,6 @@ class ServiceNodeInstanceNetworkFunctionMapping(model_base.BASEV2): status_details = sa.Column(sa.String(4096), nullable=True) -class NFPClientApi(object): - """ Client side of the NFP Framework user """ - - RPC_API_VERSION = '1.0' - - def __init__(self, topic): - target = oslo_messaging.Target( - topic=topic, version=self.RPC_API_VERSION) - self.client = n_rpc.get_client(target) - - def create_network_function(self, context, network_function): - LOG.info("Sending RPC CREATE NETWORK FUNCTION to Service " - "Orchestrator for tenant:%(tenant_id)s with " - "service profile:%(service_profile_id)s", - {'tenant_id': network_function['tenant_id'], - 'service_profile_id': network_function[ - 'service_profile']['id']}) - cctxt = self.client.prepare( - fanout=False, topic=nfp_rpc_topics.NFP_NSO_TOPIC) - return cctxt.cast( - context, - 'create_network_function', - network_function=network_function) - - def delete_network_function(self, context, network_function_id, - network_function_data): - LOG.info("Sending RPC DELETE NETWORK FUNCTION to Service " - "Orchestrator for NF:" - "%(network_function_id)s", - {'network_function_id': network_function_id}) - cctxt = self.client.prepare(version=self.RPC_API_VERSION) - return cctxt.cast( - context, - 'delete_network_function', - network_function_id=network_function_id, - network_function_data=network_function_data) - - def update_network_function(self, context, network_function_id, config): - LOG.info("Sending RPC UPDATE NETWORK FUNCTION to Service " - "Orchestrator for NF:" - "%(network_function_id)s", - {'network_function_id': network_function_id}) - cctxt = self.client.prepare(version=self.RPC_API_VERSION) - return cctxt.cast( - context, - 'update_network_function', - network_function_id=network_function_id, - config=config) - - def get_network_function(self, context, network_function_id): - LOG.debug("Sending RPC GET NETWORK FUNCTION to Service " - "Orchestrator for NF: %s", network_function_id) - cctxt = self.client.prepare(version=self.RPC_API_VERSION) - return cctxt.call( - context, - 'get_network_function', - network_function_id=network_function_id) - - def consumer_ptg_added_notification(self, context, network_function_id, - policy_target_group): - LOG.info("Sending RPC CONSUMER PTG ADDED NOTIFICATION to Service " - "Orchestrator for NF:" - "%(network_function_id)s", - {'network_function_id': network_function_id}) - cctxt = self.client.prepare(version=self.RPC_API_VERSION) - return cctxt.cast(context, - 'consumer_ptg_added_notification', - network_function_id=network_function_id, - policy_target_group=policy_target_group) - - def consumer_ptg_removed_notification(self, context, network_function_id, - policy_target_group): - LOG.info("Sending RPC CONSUMER PTG REMOVED NOTIFICATION to " - " Service Orchestrator for NF:%(network_function_id)s", - {'network_function_id': network_function_id}) - cctxt = self.client.prepare(version=self.RPC_API_VERSION) - return cctxt.cast(context, - 'consumer_ptg_removed_notification', - network_function_id=network_function_id, - policy_target_group=policy_target_group) - - def policy_target_added_notification(self, context, network_function_id, - policy_target): - LOG.info("Sending RPC POLICY TARGET ADDED NOTIFICATION to " - "Service Orchestrator for NF:%(network_function_id)s", - {'network_function_id': network_function_id}) - cctxt = self.client.prepare(version=self.RPC_API_VERSION) - return cctxt.cast(context, - 'policy_target_added_notification', - network_function_id=network_function_id, - policy_target=policy_target) - - def policy_target_removed_notification(self, context, network_function_id, - policy_target): - LOG.info("Sending RPC POLICY TARGET REMOVED NOTIFICATION to " - "Service Orchestrator for NF:%(network_function_id)s", - {'network_function_id': network_function_id}) - cctxt = self.client.prepare(version=self.RPC_API_VERSION) - return cctxt.cast(context, - 'policy_target_removed_notification', - network_function_id=network_function_id, - policy_target=policy_target) - - def get_plumbing_info(self, context, node_driver_ctxt): - LOG.info("Sending RPC GET PLUMBING INFO to Service Orchestrator ") - request_info = dict(profile=node_driver_ctxt.current_profile, - tenant_id=node_driver_ctxt.provider['tenant_id'], - provider=node_driver_ctxt.provider) - cctxt = self.client.prepare(version=self.RPC_API_VERSION) - return cctxt.call(context, 'get_plumbing_info', - request_info=request_info) - - class NFPContext(object): @staticmethod @@ -302,964 +172,3 @@ class NFPContext(object): nfp_context_store.context.update({sc_instance_id: context}) else: nfp_context_store.context = {sc_instance_id: context} - - -class NFPNodeDriver(driver_base.NodeDriverBase): - SUPPORTED_SERVICE_TYPES = [ - pconst.FIREWALL, pconst.VPN, - pconst.LOADBALANCERV2] - vendor_name = nfp_constants.NFP_VENDOR.upper() - required_heat_resources = { - pconst.LOADBALANCERV2: ['OS::Neutron::LBaaS::LoadBalancer', - 'OS::Neutron::LBaaS::Listener', - 'OS::Neutron::LBaaS::Pool'], - pconst.FIREWALL: ['OS::Neutron::Firewall', - 'OS::Neutron::FirewallPolicy'], - pconst.VPN: ['OS::Neutron::VPNService'], - } - initialized = False - - def __init__(self): - super(NFPNodeDriver, self).__init__() - self._lbaas_plugin = None - self.nfp_db = nfp_db.NFPDbBase() - - @property - def name(self): - return self._name - - def initialize(self, name): - self.initialized = True - self._name = name - if cfg.CONF.nfp_node_driver.is_service_admin_owned: - self.resource_owner_tenant_id = self._resource_owner_tenant_id() - else: - self.resource_owner_tenant_id = None - self._setup_rpc() - - def _setup_rpc(self): - self.nfp_notifier = NFPClientApi(nfp_rpc_topics.NFP_NSO_TOPIC) - - def _parse_service_flavor_string(self, service_flavor_str): - service_details = {} - if ',' not in service_flavor_str: - service_details['device_type'] = 'nova' - service_details['service_vendor'] = service_flavor_str - else: - service_flavor_dict = dict(item.split('=') for item - in service_flavor_str.split(',')) - service_details = {key.strip(): value.strip() for key, value - in six.iteritems(service_flavor_dict)} - return service_details - - def _update_node_instance_network_function_map(self, context, - updated_network_function_map): - self.nfp_db.update_node_instance_network_function_map( - context.plugin_session, - context.current_node['id'], - context.instance['id'], - updated_network_function_map) - - def get_plumbing_info(self, context): - plumbing_request = {} - context._plugin_context = self._get_resource_owner_context( - context._plugin_context) - service_type = context.current_profile['service_type'] - - service_flavor_str = context.current_profile['service_flavor'] - service_details = self._parse_service_flavor_string(service_flavor_str) - if service_details['device_type'] == 'None': - if not NFPContext.get_nfp_context(context.instance['id']): - nfp_context = NFPContext.store_nfp_context( - context.instance['id']) - return {} - # Management PTs are managed by NFP since it supports hosting multiple - # logical services in a single device - # plumber will return stitching network PT instead of consumer - # as chain is instantiated while creating provider group. - if service_type in GATEWAY_PLUMBER_TYPE: - gateway_type_node = {'service_type': service_type, - 'context': {}} - nfp_context = NFPContext.get_nfp_context(context.instance['id']) - if nfp_context: - if len(nfp_context['sc_gateway_type_nodes']): - LOG.info( - "Not requesting plumber for PTs for service type " - "%(service_type)s", {'service_type': service_type}) - if not nfp_context['update']: - nfp_context['sc_gateway_type_nodes'].append( - gateway_type_node) - NFPContext.store_nfp_context( - context.instance['id'], - sc_gateway_type_nodes=( - nfp_context['sc_gateway_type_nodes'])) - return {} - if not nfp_context['update']: - nfp_context['sc_gateway_type_nodes'].append( - gateway_type_node) - NFPContext.store_nfp_context( - context.instance['id'], - sc_gateway_type_nodes=( - nfp_context['sc_gateway_type_nodes'])) - plumbing_request = self.nfp_notifier.get_plumbing_info( - context._plugin_context, context) - else: - NFPContext.store_nfp_context( - context.instance['id'], - sc_gateway_type_nodes=[gateway_type_node]) - plumbing_request = self.nfp_notifier.get_plumbing_info( - context._plugin_context, context) - - else: # Loadbalancer which is one arm - NFPContext.store_nfp_context( - context.instance['id']) - plumbing_request = self.nfp_notifier.get_plumbing_info( - context._plugin_context, context) - - LOG.info("Requesting plumber for PTs for " - "service type %(service_type)s with " - "%(plumbing_request)s ", - {'plumbing_request': plumbing_request, - 'service_type': service_type}) - return plumbing_request - - def validate_create(self, context): - if not context.current_profile: - raise ServiceProfileRequired() - if (not context.current_profile['vendor'] or not - context.current_profile['insertion_mode'] or not - context.current_profile['service_type'] or not - context.current_profile['service_flavor']): - raise RequiredProfileAttributesNotSet() - if context.current_profile['vendor'] != self.vendor_name: - raise NodeVendorMismatch(vendor=self.vendor_name) - if (context.current_profile['insertion_mode'].lower() != - nfp_constants.L3_INSERTION_MODE): - raise UnSupportedInsertionMode() - if context.current_profile['service_type'] not in ( - self.SUPPORTED_SERVICE_TYPES): - raise InvalidServiceType() - self._is_node_order_in_spec_supported(context) - - def _validate_status(self, context): - servicechain_instance_id = context.instance['id'] - servicechain_instance = context.sc_plugin.get_servicechain_instance( - context.plugin_context, servicechain_instance_id) - if servicechain_instance['status']: - if servicechain_instance['status'] == nfp_constants.BUILD: - raise OperationNotSupported() - - def validate_update(self, context): - NFPContext.store_nfp_context(context.instance['id'], - update=True) - if not context.original_node: # PT create/delete notifications - return - if context.current_node and not context.current_profile: - raise ServiceProfileRequired() - if context.current_profile['vendor'] != self.vendor_name: - raise NodeVendorMismatch(vendor=self.vendor_name) - if (context.current_profile['insertion_mode'].lower() != - nfp_constants.L3_INSERTION_MODE): - raise UnSupportedInsertionMode() - if context.current_profile['service_type'] not in ( - self.SUPPORTED_SERVICE_TYPES): - raise InvalidServiceType() - self._validate_status(context) - - def _wait(self, thread, context): - try: - result = thread.wait() - return result - except Exception as e: - NFPContext.clear_nfp_context(context.instance['id']) - raise e - - def create(self, context): - try: - context._plugin_context = self._get_resource_owner_context( - context._plugin_context) - network_function_id = None - status = nfp_constants.BUILD - status_details = 'processing create in node driver.' - self._create_network_function(context) - except Exception: - # NFPContext.clear_nfp_context(context.instance['id']) - exc_type, exc_value, exc_traceback = sys.exc_info() - message = "Traceback: %s" % (exc_value) - LOG.error(message) - - finally: - self.nfp_db.create_node_instance_network_function_map( - context.plugin_session, context.current_node['id'], - context.instance['id'], network_function_id, - status, status_details) - LOG.info("Processed create NF in node driver." - "servicechain_instance_id: %(sci_id)s, " - "servicechain_node_id: %(scn_id)s", { - 'sci_id': context.instance['id'], - 'scn_id': context.current_node['id']}) - - def _wait_for_node_operation_completion(self, context, network_function_id, - operation): - # Check for NF status in a separate thread - LOG.debug("Spawning thread for nf ACTIVE poll operation: %s", ( - operation)) - nfp_context = NFPContext.get_nfp_context(context.instance['id']) - nfp_context['sc_node_count'] -= 1 - nfp_context['network_functions'].append(network_function_id) - # At last wait for the threads to complete, success/failure/timeout - if nfp_context['sc_node_count'] == 0: - network_functions = nfp_context['network_functions'] - for network_function in network_functions: - LOG.debug("Spawning thread for nf ACTIVE poll") - if operation == nfp_constants.DELETE: - gth = nfp_context['thread_pool'].spawn( - self._wait_for_network_function_delete_completion, - context, network_function) - else: - gth = nfp_context['thread_pool'].spawn( - self._wait_for_network_function_operation_completion, - context, network_function, operation=operation) - - nfp_context['active_threads'].append(gth) - - message = "Active Threads count (%d), sc_node_count (%d)" % ( - len(nfp_context['active_threads']), - nfp_context['sc_node_count']) - LOG.debug(message) - nfp_context['thread_pool'].waitall() - # Get the results - for gth in nfp_context['active_threads']: - self._wait(gth, context) - - NFPContext.clear_nfp_context(context.instance['id']) - else: - NFPContext.store_nfp_context(context.instance['id'], **nfp_context) - - def update(self, context): - NFPContext.clear_nfp_context(context.instance['id']) - context._plugin_context = self._get_resource_owner_context( - context._plugin_context) - network_function_map = ( - self.nfp_db.get_node_instance_network_function_map( - context.plugin_session, - context.current_node['id'], - context.instance['id'])) - - if not all([network_function_map, context.original_node.get('config'), - context.current_node.get('config')]): - return - - network_function_id = network_function_map.network_function_id - self._update(context, network_function_id) - - def _get_node_count(self, context): - current_specs = context.relevant_specs - node_list = [] - for spec in current_specs: - node_list.extend(spec['nodes']) - NFPContext.store_nfp_context(context.instance['id'], - sc_node_count=len(node_list)) - return len(node_list) - - def get_status(self, context): - context._plugin_context = self._get_resource_owner_context( - context._plugin_context) - network_function_map = ( - self.nfp_db.get_node_instance_network_function_map( - context.plugin_session, - context.current_node['id'], - context.instance['id'])) - nf_status = network_function_map.status - if nf_status not in nfp_constants.NFP_STATUS: - nf_status = nfp_constants.BUILD - status = nfp_constants.NFP_STATUS_MAP[nf_status] - return status - - def delete(self, context): - nfp_context = ( - NFPContext.get_nfp_context(context.instance['id'])) - if nfp_context and not nfp_context.get('sc_node_count'): - nfp_context['sc_node_count'] = self._get_node_count(context) - - context._plugin_context = self._get_resource_owner_context( - context._plugin_context) - network_function_map = ( - self.nfp_db.get_node_instance_network_function_map( - context.plugin_session, - context.current_node['id'], - context.instance['id'])) - network_function_id = None - if network_function_map: - updated_network_function_map = { - 'status': nfp_constants.BUILD, - 'status_details': 'node driver processing node delete'} - self._update_node_instance_network_function_map( - context, updated_network_function_map) - network_function_id = network_function_map.network_function_id - - if network_function_id: - try: - self._delete_network_function(context, network_function_id) - except Exception: - # NFPContext.clear_nfp_context(context.instance['id']) - LOG.exception("Delete Network service Failed") - exc_type, exc_value, exc_traceback = sys.exc_info() - message = "Traceback: %s" % (exc_value) - LOG.error(message) - - self._update_ptg(context) - - def update_policy_target_added(self, context, policy_target): - if context.current_profile['service_type'] == pconst.LOADBALANCERV2: - if self._is_service_target(policy_target): - return - context._plugin_context = self._get_resource_owner_context( - context._plugin_context) - network_function_map = ( - self.nfp_db.get_node_instance_network_function_map( - context.plugin_session, - context.current_node['id'], - context.instance['id'])) - if network_function_map: - updated_network_function_map = { - 'status': nfp_constants.BUILD, - 'status_details': 'node driver processing PT add'} - self._update_node_instance_network_function_map( - context, updated_network_function_map) - network_function_id = network_function_map.network_function_id - self.nfp_notifier.policy_target_added_notification( - context.plugin_context, network_function_id, policy_target) - - def update_policy_target_removed(self, context, policy_target): - if context.current_profile['service_type'] == pconst.LOADBALANCERV2: - if self._is_service_target(policy_target): - return - context._plugin_context = self._get_resource_owner_context( - context._plugin_context) - network_function_map = ( - self.nfp_db.get_node_instance_network_function_map( - context.plugin_session, - context.current_node['id'], - context.instance['id'])) - - if network_function_map: - updated_network_function_map = { - 'status': nfp_constants.BUILD, - 'status_details': 'node driver processing PT remove'} - self._update_node_instance_network_function_map( - context, updated_network_function_map) - network_function_id = network_function_map.network_function_id - self.nfp_notifier.policy_target_removed_notification( - context.plugin_context, network_function_id, policy_target) - - def notify_chain_parameters_updated(self, context): - pass # We are not using the classifier specified in redirect Rule - - def update_node_consumer_ptg_added(self, context, policy_target_group): - - # When a group is created which is both consumer and provider. - # method is invoked for stitching group too.. ignoring. - if policy_target_group.get('proxied_group_id'): - return - if context.current_profile['service_type'] == pconst.FIREWALL: - context._plugin_context = self._get_resource_owner_context( - context._plugin_context) - network_function_map = ( - self.nfp_db.get_node_instance_network_function_map( - context.plugin_session, - context.current_node['id'], - context.instance['id'])) - - if network_function_map: - updated_network_function_map = { - 'status': nfp_constants.BUILD, - 'status_details': 'node driver processing consumer add'} - self._update_node_instance_network_function_map(context, - updated_network_function_map) - network_function_id = network_function_map.network_function_id - self.nfp_notifier.consumer_ptg_added_notification( - context.plugin_context, network_function_id, - policy_target_group) - - def update_node_consumer_ptg_removed(self, context, policy_target_group): - # When a group is created which is both consumer and provider. - # method is invoked for stitching group too.. ignoring. - if policy_target_group.get('proxied_group_id'): - return - if context.current_profile['service_type'] == pconst.FIREWALL: - context._plugin_context = self._get_resource_owner_context( - context._plugin_context) - network_function_map = ( - self.nfp_db.get_node_instance_network_function_map( - context.plugin_session, - context.current_node['id'], - context.instance['id'])) - - if network_function_map: - updated_network_function_map = { - 'status': nfp_constants.BUILD, - 'status_details': 'node driver processing consumer remove'} - self._update_node_instance_network_function_map( - context, updated_network_function_map) - network_function_id = network_function_map.network_function_id - self.nfp_notifier.consumer_ptg_removed_notification( - context.plugin_context, network_function_id, - policy_target_group) - - def policy_target_group_updated(self, context, old_ptg, current_ptg): - if not (old_ptg and current_ptg): - return - if current_ptg['description']: - desc = current_ptg['description'].split(':') - if 'opflex_eoc' in desc: - if (set(old_ptg[ - 'provided_policy_rule_sets']).symmetric_difference( - set(current_ptg['provided_policy_rule_sets']))): - pts = context.gbp_plugin.get_policy_targets( - context.plugin_context, - filters={'port_id': [desc[-1]]}) - (pt,) = pts - filters = {'description': [current_ptg['description']]} - ptgs = context.gbp_plugin.get_policy_target_groups( - context.plugin_context, filters) - prs = [] - for ptg in ptgs: - prs += ptg['provided_policy_rule_sets'] - context.gbp_plugin.update_policy_target_group( - context.plugin_context, - pt['policy_target_group_id'], - {'policy_target_group': - {'provided_policy_rule_sets': - dict((x, '') for x in prs)}}) - - def _wait_for_network_function_delete_completion(self, context, - network_function_id): - # [REVISIT: (akash) do we need to do error handling here] - if not network_function_id: - return - - time_waited = 0 - network_function = None - while time_waited < cfg.CONF.nfp_node_driver.service_delete_timeout: - network_function = self.nfp_notifier.get_network_function( - context.plugin_context, network_function_id) - if not network_function or ( - network_function['status'] == nfp_constants.ERROR): - break - eventlet.sleep(5) - time_waited = time_waited + 5 - - if network_function: - LOG.error("Delete network function %(network_function)s " - "failed", - {'network_function': network_function_id}) - raise NodeInstanceDeleteFailed() - - def _wait_for_network_function_operation_completion(self, context, - network_function_id, - operation): - if not network_function_id: - raise NodeInstanceCreateFailed() - - time_waited = 0 - network_function = None - timeout = cfg.CONF.nfp_node_driver.service_create_timeout - - while time_waited < timeout: - network_function = self.nfp_notifier.get_network_function( - context.plugin_context, network_function_id) - LOG.debug("Got %s nf result for NF: %s with status:%s," - "time waited: %s", (network_function_id, operation, - time_waited, network_function['status'])) - if not network_function: - LOG.error("Failed to retrieve network function") - eventlet.sleep(5) - time_waited = time_waited + 5 - continue - else: - if time_waited == 0: - LOG.info("STARTED POLLING for %(operation)s network " - "function for NF:%(network_function_id)s " - "with initial result: %(result)s ", - {'operation': operation, - 'network_function_id': network_function_id, - 'result': network_function}) - if (network_function['status'] == nfp_constants.ACTIVE or - network_function['status'] == nfp_constants.ERROR): - LOG.info("COMPLETED POLLING for %(operation)s network " - "function for NF:%(network_function_id)s ", - {'network_function_id': network_function_id, - 'operation': operation}) - break - eventlet.sleep(5) - time_waited = time_waited + 5 - - LOG.info("Got %(operation)s network function result for NF:" - "%(network_function_id)s with status:%(status)s", - {'network_function_id': network_function_id, - 'operation': operation, - 'status': network_function['status']}) - - if network_function['status'] != nfp_constants.ACTIVE: - LOG.error("%(operation)s network function:" - "%(network_function)s " - "failed. Status: %(status)s", - {'network_function': network_function_id, - 'status': network_function['status'], - 'operation': operation}) - if operation.lower() == nfp_constants.CREATE: - raise NodeInstanceCreateFailed() - elif operation.lower() == nfp_constants.UPDATE: - raise NodeInstanceUpdateFailed() - - def _is_service_target(self, policy_target): - if policy_target['name'] and (policy_target['name'].startswith( - plumber_base.SERVICE_TARGET_NAME_PREFIX) or - policy_target['name'].startswith('tscp_endpoint_service') or - policy_target['name'].startswith('vip_pt')): - return True - else: - return False - - def _resource_owner_tenant_id(self): - user, pwd, tenant, auth_url = utils.get_keystone_creds() - keystoneclient = keyclient.Client(username=user, password=pwd, - auth_url=auth_url) - try: - tenant = keystoneclient.tenants.find(name=tenant) - return tenant.id - except k_exceptions.NotFound: - with excutils.save_and_reraise_exception(reraise=True): - LOG.error('No tenant with name %(tenant)s exists.', - {'tenant': tenant}) - except k_exceptions.NoUniqueMatch: - with excutils.save_and_reraise_exception(reraise=True): - LOG.error('Multiple tenants matches found for %(tenant)s', - {'tenant': tenant}) - - def _get_resource_owner_context(self, plugin_context): - # REVISIT(AKASH) Need to revisit as this api is not needed - # with present scenarios - ''' - if cfg.CONF.nfp_node_driver.is_service_admin_owned: - resource_owner_context = plugin_context.elevated() - resource_owner_context.tenant_id = self.resource_owner_tenant_id - user, pwd, ignore_tenant, auth_url = utils.get_keystone_creds() - keystoneclient = keyclient.Client(username=user, password=pwd, - auth_url=auth_url) - resource_owner_context.auth_token = keystoneclient.get_token( - self.resource_owner_tenant_id) - return resource_owner_context - else: - return plugin_context - ''' - return plugin_context - - def _update(self, context, network_function_id): - if (context.original_node['config'] != context.current_node['config']): - try: - updated_network_function_map = { - 'status': nfp_constants.BUILD, - 'status_details': 'node driver processing node update'} - self._update_node_instance_network_function_map( - context, updated_network_function_map) - self.nfp_notifier.update_network_function( - context.plugin_context, network_function_id, - context.current_node['config']) - except Exception: - LOG.exception("Update Network service Failed for " - "network function: %(nf_id)s", - {'nf_id': network_function_id}) - else: - LOG.info("No action to take on update") - - def _get_service_chain_specs(self, context): - current_specs = context.relevant_specs - for spec in current_specs: - filters = {'id': spec['nodes']} - nodes = context.sc_plugin.get_servicechain_nodes( - context.plugin_context, filters) - for node in nodes: - profile = context.sc_plugin.get_service_profile( - context.plugin_context, node['service_profile_id']) - node['sc_service_profile'] = profile - spec['sc_nodes'] = nodes - return current_specs - - def _sc_head_gateway_node_service_targets(self, context, - service_type, relationships): - current_specs = context.relevant_specs - service_targets = [] - for spec in current_specs: - filters = {'id': spec['nodes']} - nodes = context.sc_plugin.get_servicechain_nodes( - context.plugin_context, filters) - for node in nodes: - profile = context.sc_plugin.get_service_profile( - context.plugin_context, node['service_profile_id']) - if (profile['service_type'] != service_type and - profile['service_type'] in GATEWAY_PLUMBER_TYPE): - for relationship in relationships: - service_targets.extend(ncp_model.get_service_targets( - context.session, - servicechain_instance_id=context.instance['id'], - servicechain_node_id=node['id'], - relationship=relationship)) - return service_targets - - def _get_service_targets(self, context): - service_type = context.current_profile['service_type'] - provider_service_targets = [] - consumer_service_targets = [] - service_flavor_str = context.current_profile['service_flavor'] - service_details = self._parse_service_flavor_string(service_flavor_str) - nfp_context = NFPContext.get_nfp_context(context.instance['id']) - is_gateway_type = False - global GATEWAY_PLUMBER_TYPE - if service_type in GATEWAY_PLUMBER_TYPE: - for gateway_node in nfp_context['sc_gateway_type_nodes']: - if gateway_node['context']: - service_target_info = gateway_node['context'] - return service_target_info - is_gateway_type = True - - service_targets = context.get_service_targets() - # Bug with NCP. For create, its not setting service targets in context - - if not service_targets: - service_targets = context.get_service_targets(update=True) - - if not service_targets and is_gateway_type: - relationships = [nfp_constants.PROVIDER, nfp_constants.CONSUMER] - service_targets = self._sc_head_gateway_node_service_targets( - context, - service_type, - relationships) - - for service_target in service_targets: - if service_target.relationship == nfp_constants.CONSUMER: - consumer_service_targets.append(service_target) - elif service_target.relationship == nfp_constants.PROVIDER: - provider_service_targets.append(service_target) - - LOG.debug("provider targets: %s consumer targets %s", ( - provider_service_targets, consumer_service_targets)) - if (service_details['device_type'] != 'None' and ( - not provider_service_targets or (service_type in - [pconst.FIREWALL, pconst.VPN] and not consumer_service_targets))): - LOG.error("Service Targets are not created for the Node " - "of service_type %(service_type)s", - {'service_type': service_type}) - raise Exception(_("Service Targets are not created " - "for the Node")) - - if (not consumer_service_targets and - not provider_service_targets): - return {} - - service_target_info = { - 'provider_ports': [], - 'provider_subnet': None, - 'provider_pts': [], - 'provider_pt_objs': [], - 'provider_ptg': [], - 'consumer_ports': [], - 'consumer_subnet': None, - 'consumer_pts': [], - 'consumer_pt_objs': [], - 'consumer_ptg': []} - - for service_target in provider_service_targets: - policy_target = context.gbp_plugin.get_policy_target( - context.plugin_context, service_target.policy_target_id) - policy_target_group = context.gbp_plugin.get_policy_target_group( - context.plugin_context, - policy_target['policy_target_group_id']) - port = context.core_plugin.get_port( - context.plugin_context, policy_target['port_id']) - port['ip_address'] = port['fixed_ips'][0]['ip_address'] - subnet = context.core_plugin.get_subnet( - context.plugin_context, port['fixed_ips'][0]['subnet_id']) - service_target_info['provider_ports'].append(port) - service_target_info['provider_subnet'] = subnet - service_target_info['provider_pts'].append(policy_target['id']) - service_target_info['provider_pt_objs'].append(policy_target) - service_target_info['provider_ptg'].append(policy_target_group) - - for service_target in consumer_service_targets: - policy_target = context.gbp_plugin.get_policy_target( - context.plugin_context, service_target.policy_target_id) - policy_target_group = context.gbp_plugin.get_policy_target_group( - context.plugin_context, - policy_target['policy_target_group_id']) - port = context.core_plugin.get_port( - context.plugin_context, policy_target['port_id']) - port['ip_address'] = port['fixed_ips'][0]['ip_address'] - subnet = context.core_plugin.get_subnet( - context.plugin_context, port['fixed_ips'][0]['subnet_id']) - service_target_info['consumer_ports'].append(port) - service_target_info['consumer_subnet'] = subnet - service_target_info['consumer_pts'].append(policy_target['id']) - service_target_info['consumer_pt_objs'].append(policy_target) - service_target_info['consumer_ptg'].append(policy_target_group) - - for gateway_node in nfp_context['sc_gateway_type_nodes']: - if gateway_node['service_type'] == service_type: - gateway_node['context'] = service_target_info - NFPContext.store_nfp_context(context.instance['id'], - **nfp_context) - return service_target_info - - # Needs a better algorithm - def _is_node_order_in_spec_supported(self, context): - current_specs = context.relevant_specs - service_type_list_in_chain = [] - node_list = [] - for spec in current_specs: - node_list.extend(spec['nodes']) - - for node_id in node_list: - node_info = context.sc_plugin.get_servicechain_node( - context.plugin_context, node_id) - profile = context.sc_plugin.get_service_profile( - context.plugin_context, node_info['service_profile_id']) - service_type_list_in_chain.append(profile['service_type']) - - if len(service_type_list_in_chain) != len( - set(service_type_list_in_chain)): - raise DuplicateServiceTypeInChain() - - allowed_chain_combinations = [ - [pconst.VPN], - [pconst.VPN, pconst.FIREWALL], - [pconst.VPN, pconst.FIREWALL, pconst.LOADBALANCERV2], - [pconst.FIREWALL], - [pconst.FIREWALL, pconst.LOADBALANCERV2], - [pconst.LOADBALANCERV2]] - - if service_type_list_in_chain not in allowed_chain_combinations: - raise InvalidNodeOrderInChain( - node_order=allowed_chain_combinations) - - NFPContext.store_nfp_context(context.instance['id'], - sc_node_count=len(node_list)) - - def _get_consumers_for_provider(self, context, provider): - ''' - { - consuming_ptgs_details: [{'ptg': <>, 'subnets': <>}] - consuming_eps_details: [] - } - ''' - - consuming_ptgs = [] - consuming_ptgs_details = [] - consuming_eps_details = [] - - filters = {'id': provider['provided_policy_rule_sets']} - provided_prs = context.gbp_plugin.get_policy_rule_sets( - context.plugin_context, filters=filters) - redirect_prs = None - for prs in provided_prs: - filters = {'id': prs['policy_rules']} - policy_rules = context.gbp_plugin.get_policy_rules( - context.plugin_context, filters=filters) - for policy_rule in policy_rules: - filters = {'id': policy_rule['policy_actions'], - 'action_type': [gconst.GP_ACTION_REDIRECT]} - policy_actions = context.gbp_plugin.get_policy_actions( - context.plugin_context, filters=filters) - if policy_actions: - redirect_prs = prs - break - - if not redirect_prs: - LOG.error("Redirect rule doesn't exist in policy target rule " - " set") - return consuming_ptgs_details, consuming_eps_details - - consuming_ptg_ids = redirect_prs['consuming_policy_target_groups'] - consuming_ep_ids = redirect_prs['consuming_external_policies'] - if consuming_ptg_ids: - consuming_ptgs = context.gbp_plugin.get_policy_target_groups( - context.plugin_context, filters={'id': consuming_ptg_ids}) - if consuming_ep_ids: - consuming_eps_details = context.gbp_plugin.get_external_policies( - context.plugin_context, filters={'id': consuming_ep_ids}) - - for ptg in consuming_ptgs: - subnet_ids = ptg['subnets'] - subnets = context.core_plugin.get_subnets( - context.plugin_context, filters={'id': subnet_ids}) - consuming_ptgs_details.append({'ptg': ptg, 'subnets': subnets}) - - return consuming_ptgs_details, consuming_eps_details - - def _get_nfp_network_function(self, context): - """ - nfp_nf_data :- - - {'resource_owner_context': <>, - 'service_chain_instance': <>, - 'service_chain_node': <>, - 'service_profile': <>, - 'service_config': context.current_node.get('config'), - 'provider': {'pt':<>, 'ptg':<>, 'port':<>, 'subnet':<>}, - 'consumer': {'pt':<>, 'ptg':<>, 'port':<>, 'subnet':<>}, - 'management': {'pt':<>, 'ptg':<>, 'port':<>, 'subnet':<>}, - 'management_ptg_id': <>, - 'network_function_mode': nfp_constants.GBP_MODE, - 'tenant_id': <>, - 'consuming_ptgs_details': [], - 'consuming_eps_details': [] - } - - """ - nfp_nf_data = {} - - sc_instance = context.instance - service_targets = self._get_service_targets(context) - - consuming_ptgs_details = [] - consuming_eps_details = [] - if service_targets: - consuming_ptgs_details, consuming_eps_details = \ - self._get_consumers_for_provider(context, - service_targets['provider_ptg'][0]) - - if context.current_profile['service_type'] == pconst.LOADBALANCERV2: - config_param_values = sc_instance.get('config_param_values', {}) - if config_param_values: - config_param_values = jsonutils.loads(config_param_values) - vip_ip = config_param_values.get('vip_ip') - if not vip_ip: - raise VipNspNotSetonProvider() - - if service_targets: - for provider_port in service_targets['provider_ports']: - provider_port['allowed_address_pairs'] = [ - {'ip_address': vip_ip}] - port = { - 'port': provider_port - } - context.core_plugin.update_port( - context.plugin_context, provider_port['id'], port) - - provider = { - 'pt': service_targets.get('provider_pt_objs', []), - 'ptg': service_targets.get('provider_ptg', []), - 'port': service_targets.get('provider_ports', []), - 'subnet': service_targets.get('provider_subnet', None), - 'port_model': nfp_constants.GBP_PORT, - 'port_classification': nfp_constants.PROVIDER} - - consumer_pt = None - consumer_ptg = None - consumer_ports = None - - if service_targets.get('consumer_pt_objs'): - consumer_pt = service_targets.get('consumer_pt_objs') - if service_targets.get('consumer_ptg'): - consumer_ptg = service_targets.get('consumer_ptg') - if service_targets.get('consumer_ports'): - consumer_ports = service_targets.get('consumer_ports') - - consumer = { - 'pt': consumer_pt, - 'ptg': consumer_ptg, - 'port': consumer_ports, - 'subnet': service_targets.get('consumer_subnet', None), - 'port_model': nfp_constants.GBP_PORT, - 'port_classification': nfp_constants.CONSUMER} - - management = { - 'pt': None, - 'ptg': None, - 'port': None, - 'subnet': None, - 'port_model': nfp_constants.GBP_NETWORK, - 'port_classification': nfp_constants.MANAGEMENT} - - service_chain_specs = self._get_service_chain_specs(context) - - nfp_nf_data = { - 'resource_owner_context': context._plugin_context.to_dict(), - 'service_chain_instance': sc_instance, - 'service_chain_node': context.current_node, - 'service_profile': context.current_profile, - 'service_config': context.current_node.get('config'), - 'provider': provider, - 'consumer': consumer, - 'management': management, - 'management_ptg_id': sc_instance['management_ptg_id'], - 'network_function_mode': nfp_constants.GBP_MODE, - 'tenant_id': context.provider['tenant_id'], - 'consuming_ptgs_details': consuming_ptgs_details, - 'consuming_eps_details': consuming_eps_details, - 'service_chain_specs': service_chain_specs} - return nfp_nf_data - - def _create_network_function(self, context): - nfp_create_nf_data = self._get_nfp_network_function(context) - LOG.info("Received Call CREATE NETWORK FUNCTION for tenant: " - "%(tenant_id)s with service profile:" - "%(service_profile)s", - {'tenant_id': nfp_create_nf_data['tenant_id'], - 'service_profile': nfp_create_nf_data['service_profile']}) - self.nfp_notifier.create_network_function( - context.plugin_context, nfp_create_nf_data) - - def _delete_network_function(self, context, network_function_id): - nfp_delete_nf_data = self._get_nfp_network_function(context) - self._detach_port_from_pts(context, - nfp_delete_nf_data['provider']['pt']) - if nfp_delete_nf_data['consumer'].get('pt'): - self._detach_port_from_pts(context, - nfp_delete_nf_data['consumer']['pt']) - LOG.info("Received Call DELETE NETWORK FUNCTION for tenant: " - "%(tenant_id)s with service profile:" - "%(service_profile)s", - {'tenant_id': nfp_delete_nf_data['tenant_id'], - 'service_profile': nfp_delete_nf_data['service_profile']}) - self.nfp_notifier.delete_network_function( - context.plugin_context, network_function_id, nfp_delete_nf_data) - - def _detach_port_from_pts(self, context, policy_targets): - ''' - Disassociate port from policy target - ''' - try: - for pt in policy_targets: - context.gbp_plugin.update_policy_target( - context.plugin_context, pt['id'], - {'policy_target': {'port_id': None}}) - LOG.debug('Detached port %(port)s from pt %(pt)s', - {'pt': pt['id'], 'port': pt['port_id']}) - - except Exception: - LOG.warning("Failed to disassociate port from" - " pt: %(pt)s, Error: %(exc)s", {'pt': pt, 'exc': exc}) - - def _update_ptg(self, context): - if hasattr(context, 'provider') and context.provider['description']: - gateway_desc = 'opflex_eoc' in context.provider[ - 'description'].split(':') - if gateway_desc: - pts = context.gbp_plugin.get_policy_targets( - context.plugin_context, - filters={'port_id': [context.provider[ - 'description'].split(':')][-1]}) - (pt,) = pts - filters = {'description': [context.provider['description']]} - ptgs = context.gbp_plugin.get_policy_target_groups( - context.plugin_context, filters) - prs = [] - for ptg in ptgs: - prs += ptg['provided_policy_rule_sets'] - context.gbp_plugin.update_policy_target_group( - context.plugin_context, - pt['policy_target_group_id'], - {'policy_target_group': - {'provided_policy_rule_sets': - dict((x, '') for x in prs)}}) diff --git a/gbpservice/neutron/services/servicechain/plugins/ncp/node_plumbers/admin_owned_resources_apic_tscp.py b/gbpservice/neutron/services/servicechain/plugins/ncp/node_plumbers/admin_owned_resources_apic_tscp.py deleted file mode 100644 index 224c7ceac..000000000 --- a/gbpservice/neutron/services/servicechain/plugins/ncp/node_plumbers/admin_owned_resources_apic_tscp.py +++ /dev/null @@ -1,93 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from keystoneclient import exceptions as k_exceptions -from keystoneclient.v2_0 import client as keyclient -from oslo_config import cfg -from oslo_log import log as logging -from oslo_utils import excutils - -from gbpservice._i18n import _ -from gbpservice.common import utils -from gbpservice.neutron.services.servicechain.plugins.ncp.node_plumbers \ - import traffic_stitching_plumber as tscp - -LOG = logging.getLogger(__name__) - -TSCP_OPTS = [ - cfg.StrOpt('plumbing_resource_owner_user', - help=_("Username of the Openstack keystone user who owns the " - "resources created by the traffic stitching plumber")), - cfg.StrOpt('plumbing_resource_owner_password', - help=_("Openstack keystone password for the user who " - "owns the resources created by the traffic stitching " - "plumber"), - secret=True), - cfg.StrOpt('plumbing_resource_owner_tenant_name', - help=_("Name of the Tenant that will own the plumber created " - " resources"),) -] - -cfg.CONF.register_opts(TSCP_OPTS, "admin_owned_resources_apic_tscp") - - -class AdminOwnedResourcesApicTSCP(tscp.TrafficStitchingPlumber): - """Traffic Stitching Plumber for APIC with Admin owned resources. - - This plumber for APIC mapping provides the ability to choose the user and - who owns the resources created by the plumber. - """ - - def initialize(self): - self._resource_owner_tenant_id = None - super(AdminOwnedResourcesApicTSCP, self).initialize() - - @property - def resource_owner_tenant_id(self): - if not self._resource_owner_tenant_id: - self._resource_owner_tenant_id = ( - self._get_resource_owner_tenant_id()) - return self._resource_owner_tenant_id - - def plug_services(self, context, deployment): - context = self._get_resource_owner_context(context) - super(AdminOwnedResourcesApicTSCP, self).plug_services( - context, deployment) - - def unplug_services(self, context, deployment): - context = self._get_resource_owner_context(context) - super(AdminOwnedResourcesApicTSCP, self).unplug_services( - context, deployment) - - def _get_resource_owner_tenant_id(self): - user, pwd, tenant, auth_url = utils.get_keystone_creds() - keystoneclient = keyclient.Client(username=user, password=pwd, - auth_url=auth_url) - try: - tenant = keystoneclient.tenants.find(name=tenant) - return tenant.id - except k_exceptions.NotFound: - with excutils.save_and_reraise_exception(reraise=True): - LOG.error('No tenant with name %s exists.', tenant) - except k_exceptions.NoUniqueMatch: - with excutils.save_and_reraise_exception(reraise=True): - LOG.error('Multiple tenants matches found for %s', tenant) - - def _get_resource_owner_context(self, context): - resource_owner_context = context.elevated() - resource_owner_context.tenant_id = self.resource_owner_tenant_id - user, pwd, _, auth_url = utils.get_keystone_creds() - keystoneclient = keyclient.Client(username=user, password=pwd, - auth_url=auth_url) - resource_owner_context.auth_token = keystoneclient.get_token( - self.resource_owner_tenant_id) - return resource_owner_context diff --git a/gbpservice/neutron/services/servicechain/plugins/ncp/node_plumbers/traffic_stitching_plumber.py b/gbpservice/neutron/services/servicechain/plugins/ncp/node_plumbers/traffic_stitching_plumber.py deleted file mode 100644 index 92487c1a8..000000000 --- a/gbpservice/neutron/services/servicechain/plugins/ncp/node_plumbers/traffic_stitching_plumber.py +++ /dev/null @@ -1,186 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from neutron_lib import constants -from neutron_lib.plugins import directory -from oslo_config import cfg -from oslo_log import log as logging - -from gbpservice.neutron.extensions import driver_proxy_group as pg_ext -from gbpservice.neutron.extensions import group_policy -from gbpservice.neutron.services.grouppolicy.common import exceptions as exc -from gbpservice.neutron.services.servicechain.plugins.ncp.node_plumbers \ - import common -from gbpservice.neutron.services.servicechain.plugins.ncp import plumber_base - -LOG = logging.getLogger(__name__) -TSCP_RESOURCE_PREFIX = 'tscp_' - - -class TrafficStitchingPlumber(plumber_base.NodePlumberBase): - """Traffic Stitching Plumber (TScP). - - uses the GBP underlying constructs in order to guarantee a correct traffic - flow across services from their provider to the consumer and vice versa. - The output of the plumbing operations will be either the creation or - deletion of a set of Service Targets, which effectively result in creation - of Policy Targets exposed to the specific Node Driver for its own use. - In addition to that, TScP will create a set of L2Ps and/or PTGs that are - "stitched" together and host the actual service PTs. The proxy_group - extension is a requirement for this plumber to work. - """ - def initialize(self): - self._gbp_plugin = None - self._sc_plugin = None - - # Verify that proxy_group extension is loaded - if pg_ext.PROXY_GROUP not in cfg.CONF.group_policy.extension_drivers: - LOG.error("proxy_group GBP driver extension is mandatory for " - "traffic stitching plumber.") - raise exc.GroupPolicyDeploymentError() - - @property - def gbp_plugin(self): - if not self._gbp_plugin: - self._gbp_plugin = directory.get_plugin("GROUP_POLICY") - return self._gbp_plugin - - @property - def sc_plugin(self): - if not self._sc_plugin: - self._sc_plugin = directory.get_plugin("SERVICECHAIN") - return self._sc_plugin - - def plug_services(self, context, deployment): - if deployment: - provider = deployment[0]['context'].provider - management = deployment[0]['context'].management - # Sorted from provider (N) to consumer (0) - # TODO(ivar): validate number of interfaces per service per service - # type is as expected - self._sort_deployment(deployment) - for part in deployment: - info = part['plumbing_info'] - if not info: - continue - part_context = part['context'] - # Management PT can be created immediately - self._create_service_target( - context, part_context, info.get('management', []), - management, 'management') - # Create proper PTs based on the service type - jump_ptg = None - LOG.info("Plumbing service of type '%s'", - info['plumbing_type']) - if info['plumbing_type'] == common.PLUMBING_TYPE_ENDPOINT: - # No stitching needed, only provider side PT is created. - # overriding PT name in order to keep port security up - # for this kind of service. - node = part_context.current_node - instance = part_context.instance - for provider_info in info.get('provider', []): - provider_info['name'] = ("tscp_endpoint_service_%s_%s" - % (node['id'][:5], instance['id'][:5])) - self._create_service_target( - context, part_context, info.get('provider', []), - provider, 'provider') - - elif info['plumbing_type'] == common.PLUMBING_TYPE_GATEWAY: - # L3 stitching needed, provider and consumer side PTs are - # created. One proxy_gateway is needed in consumer side - jump_ptg = self._create_l3_jump_group( - context, provider, part['context'].current_position) - # On provider side, this service is the default gateway - info['provider'][0]['group_default_gateway'] = True - self._create_service_target( - context, part_context, info['provider'], - provider, 'provider') - # On consumer side, this service is the proxy gateway - info['consumer'][0]['proxy_gateway'] = True - self._create_service_target( - context, part_context, info['consumer'], jump_ptg, - 'consumer') - elif info['plumbing_type'] == common.PLUMBING_TYPE_TRANSPARENT: - # L2 stitching needed, provider and consumer side PTs are - # created - self._create_service_target( - context, part_context, info.get('provider', []), - provider, 'provider') - jump_ptg = self._create_l2_jump_group( - context, provider, part['context'].current_position) - self._create_service_target( - context, part_context, info['consumer'], - jump_ptg, 'consumer') - else: - LOG.warning("Unsupported plumbing type %s", - info['plumbing_type']) - # Replace current "provider" with jump ptg if needed - provider = jump_ptg or provider - - def unplug_services(self, context, deployment): - # Sorted from provider (0) to consumer (N) - if not deployment: - return - self._sort_deployment(deployment) - provider = deployment[0]['context'].provider - - for part in deployment: - self._delete_service_targets(context, part) - - # Delete jump PTGs - jump_ptgs = [] - while provider['proxy_group_id']: - try: - proxy = self.gbp_plugin.get_policy_target_group( - context, provider['proxy_group_id']) - jump_ptgs.append(proxy) - except group_policy.PolicyTargetGroupNotFound as ex: - LOG.info(ex.message) - # If this proxy doesn't exist, then subsequent ones won't too - break - provider = proxy - - for jump_ptg in reversed(jump_ptgs): - try: - self.gbp_plugin.delete_policy_target_group( - context, jump_ptg['id']) - except group_policy.PolicyTargetGroupNotFound as ex: - LOG.info(ex.message) - - def _create_l3_jump_group(self, context, proxied, position): - return self._create_jump_group( - context, proxied, position, pg_ext.PROXY_TYPE_L3) - - def _create_l2_jump_group(self, context, proxied, position): - return self._create_jump_group( - context, proxied, position, pg_ext.PROXY_TYPE_L2) - - def _create_jump_group(self, context, proxied, position, type): - data = { - "name": (TSCP_RESOURCE_PREFIX + str(position) + "_" + - proxied['name']), - "description": "Implicitly created stitching group", - "l2_policy_id": None, - "proxied_group_id": proxied['id'], - "proxy_type": type, - "proxy_group_id": constants.ATTR_NOT_SPECIFIED, - "network_service_policy_id": None, - "service_management": False - } - return self.gbp_plugin.create_policy_target_group( - context, {'policy_target_group': data}) - - def _create_service_target(self, *args, **kwargs): - kwargs['extra_data'] = {'proxy_gateway': False, - 'group_default_gateway': False} - super(TrafficStitchingPlumber, self)._create_service_target( - *args, **kwargs) diff --git a/gbpservice/neutron/tests/unit/services/servicechain/__init__.py-disabled b/gbpservice/neutron/tests/unit/services/servicechain/__init__.py-disabled deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/neutron/tests/unit/services/servicechain/base_test_servicechain_plugin.py b/gbpservice/neutron/tests/unit/services/servicechain/base_test_servicechain_plugin.py deleted file mode 100644 index 95521b575..000000000 --- a/gbpservice/neutron/tests/unit/services/servicechain/base_test_servicechain_plugin.py +++ /dev/null @@ -1,300 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or -# implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import ast -import collections - -from neutron.common import config -from neutron_lib import context as n_ctx -from oslo_config import cfg -from oslo_serialization import jsonutils - -from gbpservice.neutron.services.servicechain.plugins.ncp import ( - plugin as ncp_plugin) -from gbpservice.neutron.services.servicechain.plugins.ncp import context -from gbpservice.neutron.tests.unit.db.grouppolicy import ( - test_servicechain_db as test_servicechain_db) -from gbpservice.neutron.tests.unit.db.grouppolicy import test_group_policy_db - -cfg.CONF.import_opt( - 'node_drivers', - 'gbpservice.neutron.services.servicechain.plugins.ncp.config', - group='node_composition_plugin') - - -class ServiceChainNCPTestPlugin(ncp_plugin.NodeCompositionPlugin): - - supported_extension_aliases = ['servicechain'] + ( - test_group_policy_db.UNSUPPORTED_REQUIRED_EXTS) - path_prefix = "/servicechain" - - -SC_PLUGIN_KLASS = (ServiceChainNCPTestPlugin.__module__ + '.' + - ServiceChainNCPTestPlugin.__name__) - - -class ServiceChainPluginTestCase(test_servicechain_db.ServiceChainDbTestCase): - - def setUp(self, core_plugin=None, sc_plugin=None, gp_plugin=None): - super(ServiceChainPluginTestCase, self).setUp(core_plugin=core_plugin, - sc_plugin=sc_plugin or - SC_PLUGIN_KLASS, - gp_plugin=gp_plugin) - try: - config.cfg.CONF.keystone_authtoken.username - except config.cfg.NoSuchOptError: - config.cfg.CONF.register_opt( - config.cfg.StrOpt('username'), - 'keystone_authtoken') - try: - config.cfg.CONF.keystone_authtoken.password - except config.cfg.NoSuchOptError: - config.cfg.CONF.register_opt( - config.cfg.StrOpt('password'), - 'keystone_authtoken') - try: - config.cfg.CONF.keystone_authtoken.project_name - except config.cfg.NoSuchOptError: - config.cfg.CONF.register_opt( - config.cfg.StrOpt('project_name'), - 'keystone_authtoken') - - -class BaseTestGroupPolicyPluginGroupResources( - ServiceChainPluginTestCase, - test_servicechain_db.TestServiceChainResources): - - def test_spec_shared(self): - # Shared spec can only point shared nodes - node = self._create_profiled_servicechain_node( - 'LOADBALANCERV2', shared=True, shared_profile=True, - profile_tenant_id='admin', tenant_id='admin')['servicechain_node'] - self.create_servicechain_spec(nodes=[node['id']], shared=True, - expected_res_status=201) - self.create_servicechain_spec(nodes=[node['id']], shared=False, - tenant_id='admin', - expected_res_status=201) - - node = self._create_profiled_servicechain_node( - 'LOADBALANCERV2', shared=False, profile_tenant_id='nonadmin', - tenant_id='nonadmin')['servicechain_node'] - self.create_servicechain_spec(nodes=[node['id']], shared=True, - expected_res_status=404) - self.create_servicechain_spec(nodes=[node['id']], shared=True, - tenant_id='nonadmin', - expected_res_status=400) - self.create_servicechain_spec(nodes=[node['id']], shared=False, - tenant_id='nonadmin', - expected_res_status=201) - - def test_node_shared(self): - # Shared node can only point shared profile - prof = self.create_service_profile( - service_type='LOADBALANCERV2', shared=True, - tenant_id='admin')['service_profile'] - to_update = self.create_servicechain_node( - service_profile_id=prof['id'], shared=True, - expected_res_status=201)['servicechain_node'] - self.create_servicechain_node( - service_profile_id=prof['id'], shared=False, tenant_id='admin', - expected_res_status=201) - - prof = self.create_service_profile( - service_type='LOADBALANCERV2', shared=False, - tenant_id='admin')['service_profile'] - self.create_servicechain_node( - service_profile_id=prof['id'], shared=True, - expected_res_status=404) - self.create_servicechain_node( - service_profile_id=prof['id'], shared=True, - tenant_id='admin', expected_res_status=400) - self.create_servicechain_node( - service_profile_id=prof['id'], shared=False, - tenant_id='admin', expected_res_status=201) - - self.create_servicechain_spec(nodes=[to_update['id']], shared=True, - tenant_id='nonadmin', - expected_res_status=201) - - data = {'servicechain_node': {'shared': False}} - req = self.new_update_request('servicechain_nodes', data, - to_update['id']) - res = req.get_response(self.ext_api) - self.assertEqual(400, res.status_int) - res = self.deserialize(self.fmt, res) - self.assertEqual('InvalidSharedAttributeUpdate', - res['NeutronError']['type']) - - def test_profile_shared(self): - prof = self.create_service_profile( - service_type='LOADBALANCERV2', shared=True, - tenant_id='admin')['service_profile'] - self.create_servicechain_node( - service_profile_id=prof['id'], shared=True, - expected_res_status=201) - - data = {'service_profile': {'shared': False}} - req = self.new_update_request('service_profiles', data, - prof['id']) - res = req.get_response(self.ext_api) - self.assertEqual(400, res.status_int) - res = self.deserialize(self.fmt, res) - self.assertEqual('InvalidSharedAttributeUpdate', - res['NeutronError']['type']) - - prof = self.create_service_profile( - service_type='LOADBALANCERV2', shared=False)['service_profile'] - self.create_servicechain_node( - service_profile_id=prof['id'], shared=False, - expected_res_status=201) - - data = {'service_profile': {'shared': True}} - req = self.new_update_request('service_profiles', data, - prof['id']) - res = req.get_response(self.ext_api) - self.assertEqual(200, res.status_int) - res = self.deserialize(self.fmt, res) - self.assertTrue(res['service_profile']['shared']) - - def test_node_context_profile(self): - - # Current node with profile - plugin_context = n_ctx.get_admin_context() - plugin_context.is_admin = plugin_context.is_advsvc = False - plugin_context.tenant_id = self._tenant_id - - prof = self.create_service_profile( - service_type='LOADBALANCERV2')['service_profile'] - current = self.create_servicechain_node( - service_profile_id=prof['id'], - expected_res_status=201)['servicechain_node'] - ctx = context.NodeDriverContext(self.plugin, plugin_context, - None, None, current, 0, - prof, None) - - self.assertIsNone(ctx.original_node) - self.assertIsNone(ctx.original_profile) - self.assertEqual(ctx.current_node, current) - self.assertEqual(ctx.current_profile, prof) - - # Original node with profile - - prof2 = self.create_service_profile( - service_type='LOADBALANCERV2')['service_profile'] - original = self.create_servicechain_node( - service_profile_id=prof2['id'], - expected_res_status=201)['servicechain_node'] - ctx = context.NodeDriverContext( - self.plugin, plugin_context, None, None, current, 0, - prof, None, original_service_chain_node=original, - original_service_profile=prof2) - - self.assertEqual(ctx.original_node, original) - self.assertEqual(ctx.original_profile, prof2) - self.assertEqual(ctx.current_node, current) - self.assertEqual(ctx.current_profile, prof) - - def test_node_context_no_profile(self): - - plugin_context = n_ctx.get_admin_context() - plugin_context.is_admin = plugin_context.is_advsvc = False - plugin_context.tenant_id = 'test_tenant' - - current = self.create_servicechain_node( - service_type='TEST', - expected_res_status=201)['servicechain_node'] - ctx = context.NodeDriverContext(self.plugin, plugin_context, - None, None, current, 0, - None, None) - - self.assertIsNone(ctx.original_node) - self.assertIsNone(ctx.original_profile) - self.assertEqual(ctx.current_node, current) - self.assertIsNone(ctx.current_profile) - - original = self.create_servicechain_node( - service_type='TEST', - expected_res_status=201)['servicechain_node'] - ctx = context.NodeDriverContext( - self.plugin, plugin_context, None, None, current, 0, - None, None, original_service_chain_node=original) - - self.assertEqual(ctx.original_node, original) - self.assertIsNone(ctx.original_profile) - self.assertEqual(ctx.current_node, current) - self.assertIsNone(ctx.current_profile) - - def test_spec_parameters(self): - params_node_1 = ['p1', 'p2', 'p3'] - params_node_2 = ['p4', 'p5', 'p6'] - params_node_3 = ['p7', 'p8', 'p9'] - - def params_dict(params): - return jsonutils.dumps({'Parameters': - dict((x, {}) for x in params)}) - - prof = self.create_service_profile( - service_type='LOADBALANCERV2', shared=True, - tenant_id='admin')['service_profile'] - - # Create 2 nodes with different parameters - node1 = self.create_servicechain_node( - service_profile_id=prof['id'], shared=True, - config=params_dict(params_node_1), - expected_res_status=201)['servicechain_node'] - node2 = self.create_servicechain_node( - service_profile_id=prof['id'], shared=True, - config=params_dict(params_node_2), - expected_res_status=201)['servicechain_node'] - - # Create SC spec with the nodes assigned - spec = self.create_servicechain_spec( - nodes=[node1['id'], node2['id']], shared=True, - expected_res_status=201)['servicechain_spec'] - - # Verify param names correspondence - self.assertEqual( - collections.Counter(params_node_1 + params_node_2), - collections.Counter(ast.literal_eval(spec['config_param_names']))) - - # Update the spec removing one node - self.update_servicechain_spec(spec['id'], nodes=[node1['id']], - expected_res_status=200) - - spec = self.show_servicechain_spec(spec['id'])['servicechain_spec'] - # Verify param names correspondence - self.assertEqual( - collections.Counter(params_node_1), - collections.Counter(ast.literal_eval(spec['config_param_names']))) - - # Update the spec without modifying the node list - self.update_servicechain_spec(spec['id'], - name='new_name', - expected_res_status=200) - - spec = self.show_servicechain_spec(spec['id'])['servicechain_spec'] - # Verify param names correspondence - self.assertEqual( - collections.Counter(params_node_1), - collections.Counter(ast.literal_eval(spec['config_param_names']))) - - # Update a node with new config params - self.update_servicechain_node(node1['id'], - config=params_dict(params_node_3), - expected_res_status=200) - - spec = self.show_servicechain_spec(spec['id'])['servicechain_spec'] - # Verify param names correspondence - self.assertEqual( - collections.Counter(params_node_3), - collections.Counter(ast.literal_eval(spec['config_param_names']))) diff --git a/gbpservice/neutron/tests/unit/services/servicechain/ncp/__init__.py b/gbpservice/neutron/tests/unit/services/servicechain/ncp/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/neutron/tests/unit/services/servicechain/ncp/test_heat_node_driver.py b/gbpservice/neutron/tests/unit/services/servicechain/ncp/test_heat_node_driver.py deleted file mode 100644 index 49134b1af..000000000 --- a/gbpservice/neutron/tests/unit/services/servicechain/ncp/test_heat_node_driver.py +++ /dev/null @@ -1,624 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or -# implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import copy - -from unittest import mock - -import heatclient -from neutron_lib.api.definitions import external_net -from neutron_lib import context as neutron_context -from neutron_lib.plugins import constants -from oslo_serialization import jsonutils -from oslo_utils import uuidutils -import webob - -from gbpservice.neutron.services.servicechain.plugins.ncp import config -from gbpservice.neutron.services.servicechain.plugins.ncp.node_drivers import ( - heat_node_driver as heat_node_driver) -from gbpservice.neutron.services.servicechain.plugins.ncp.node_drivers import ( - openstack_heat_api_client as heatClient) -from gbpservice.neutron.tests.unit.services.grouppolicy import ( - test_resource_mapping as test_gp_driver) -from gbpservice.neutron.tests.unit.services.servicechain.ncp import ( - test_ncp_plugin as test_ncp_plugin) - - -STACK_ACTION_WAIT_TIME = 15 - - -class MockStackObject(object): - def __init__(self, status): - self.stack_status = status - - -class MockHeatClientFunctionsDeleteNotFound(object): - def delete(self, stack_id): - raise heatclient.exc.HTTPNotFound() - - def create(self, **fields): - return {'stack': {'id': uuidutils.generate_uuid()}} - - def get(self, stack_id): - return MockStackObject('DELETE_COMPLETE') - - -class MockHeatClientFunctions(object): - def delete(self, stack_id): - pass - - def create(self, **fields): - return {'stack': {'id': uuidutils.generate_uuid()}} - - def get(self, stack_id): - return MockStackObject('DELETE_COMPLETE') - - def update(self, *args, **fields): - return {'stack': {'id': uuidutils.generate_uuid()}} - - -class MockHeatClientDeleteNotFound(object): - def __init__(self, api_version, endpoint, **kwargs): - self.stacks = MockHeatClientFunctionsDeleteNotFound() - - -class MockHeatClient(object): - def __init__(self, api_version, endpoint, **kwargs): - self.stacks = MockHeatClientFunctions() - self.resources = mock.MagicMock() - - -class HeatNodeDriverTestCase( - test_ncp_plugin.NodeCompositionPluginTestCase): - - DEFAULT_LB_CONFIG_DICT = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "test_pool": { - "Type": "OS::Neutron::LBaaS::Pool", - "Properties": { - "description": "Haproxy pool from template", - "lb_algorithm": "ROUND_ROBIN", - "protocol": "HTTP", - 'listener': {'get_resource': 'listener'}, - } - }, - "test_listener": { - "Type": "OS::Neutron::LBaaS::Listener", - "Properties": { - "protocol": "HTTP", - "protocol_port": 80, - } - }, - "test_lb": { - "Type": "OS::Neutron::LBaaS::LoadBalancer", - "Properties": { - "provider": 'haproxy', - 'vip_address': '1.1.1.1', - 'vip_subnet': '1.1.1.0/24', - } - } - } - } - DEFAULT_LB_CONFIG = jsonutils.dumps(DEFAULT_LB_CONFIG_DICT) - DEFAULT_FW_CONFIG_DICT = { - "heat_template_version": "2013-05-23", - "resources": { - 'test_fw': { - "type": "OS::Neutron::Firewall", - "properties": { - "admin_state_up": True, - "firewall_policy_id": { - "get_resource": "Firewall_policy"}, - "name": "testFirewall", - "description": "test Firewall" - } - }, - 'test_fw_policy': { - "type": "OS::Neutron::FirewallPolicy", - "properties": { - "shared": False, - "description": "test firewall policy", - "name": "testFWPolicy", - "firewall_rules": [{ - "get_resource": "Rule_1"}], - "audited": True - } - } - } - } - DEFAULT_FW_CONFIG = jsonutils.dumps(DEFAULT_FW_CONFIG_DICT) - SERVICE_PROFILE_VENDOR = 'heat_based_node_driver' - - def setUp(self): - config.cfg.CONF.set_override('stack_action_wait_time', - STACK_ACTION_WAIT_TIME, - group='heat_node_driver') - mock.patch(heatclient.__name__ + ".client.Client", - new=MockHeatClient).start() - super(HeatNodeDriverTestCase, self).setUp( - node_drivers=['heat_node_driver'], - node_plumber='stitching_plumber', - core_plugin=test_gp_driver.CORE_PLUGIN) - - def _create_network(self, fmt, name, admin_state_up, **kwargs): - """Override the routine for allowing the router:external attribute.""" - # attributes containing a colon should be passed with - # a double underscore - new_args = dict(zip([x.replace('__', ':') for x in kwargs], - list(kwargs.values()))) - arg_list = new_args.pop('arg_list', ()) + (external_net.EXTERNAL,) - return super(HeatNodeDriverTestCase, self)._create_network( - fmt, name, admin_state_up, arg_list=arg_list, **new_args) - - def test_manager_initialized(self): - mgr = self.plugin.driver_manager - self.assertIsInstance(mgr.ordered_drivers[0].obj, - heat_node_driver.HeatNodeDriver) - for driver in mgr.ordered_drivers: - self.assertTrue(driver.obj.initialized) - - def _create_profiled_servicechain_node( - self, service_type=constants.LOADBALANCERV2, shared_profile=False, - profile_tenant_id=None, profile_id=None, **kwargs): - if not profile_id: - prof = self.create_service_profile( - service_type=service_type, - shared=shared_profile, - vendor=self.SERVICE_PROFILE_VENDOR, - tenant_id=profile_tenant_id or self._tenant_id)[ - 'service_profile'] - else: - prof = self.get_service_profile(profile_id) - - service_config = kwargs.get('config') - if not service_config or service_config == '{}': - if service_type == constants.FIREWALL: - kwargs['config'] = self.DEFAULT_FW_CONFIG - else: - kwargs['config'] = self.DEFAULT_LB_CONFIG - return self.create_servicechain_node( - service_profile_id=prof['id'], **kwargs) - - -class TestServiceChainInstance(HeatNodeDriverTestCase): - - def _get_node_instance_stacks(self, sc_node_id): - context = neutron_context.get_admin_context() - with context.session.begin(subtransactions=True): - return (context.session.query( - heat_node_driver.ServiceNodeInstanceStack). - filter_by(sc_node_id=sc_node_id). - all()) - - def test_invalid_service_type_rejected(self): - node_used = self._create_profiled_servicechain_node( - service_type="test")['servicechain_node'] - spec_used = self.create_servicechain_spec( - nodes=[node_used['id']])['servicechain_spec'] - provider = self.create_policy_target_group()['policy_target_group'] - classifier = self.create_policy_classifier()['policy_classifier'] - res = self.create_servicechain_instance( - provider_ptg_id=provider['id'], - classifier_id=classifier['id'], - servicechain_specs=[spec_used['id']], - expected_res_status=webob.exc.HTTPBadRequest.code) - self.assertEqual('NoDriverAvailableForAction', - res['NeutronError']['type']) - - def test_node_create(self): - with mock.patch.object(heatClient.HeatClient, - 'create') as stack_create: - stack_create.return_value = {'stack': { - 'id': uuidutils.generate_uuid()}} - self._create_simple_service_chain() - expected_stack_name = mock.ANY - expected_stack_params = mock.ANY - stack_create.assert_called_once_with( - expected_stack_name, - self.DEFAULT_LB_CONFIG_DICT, - expected_stack_params) - - def _get_pool_member_resource_dict(self, port): - member_ip = port['fixed_ips'][0]['ip_address'] - member_name = 'mem-' + member_ip - member = {member_name: { - 'Type': 'OS::Neutron::LBaaS::PoolMember', - 'Properties': { - 'subnet': {'get_param': 'Subnet'}, - 'weight': 1, - 'admin_state_up': True, - 'address': member_ip, - 'protocol_port': {'get_param': 'app_port'}, - 'pool': {'Ref': 'test_pool'} - } - } - } - return member - - def _create_policy_target_port(self, policy_target_group_id): - pt = self.create_policy_target( - policy_target_group_id=policy_target_group_id)['policy_target'] - req = self.new_show_request('ports', pt['port_id'], fmt=self.fmt) - port = self.deserialize(self.fmt, - req.get_response(self.api))['port'] - return (pt, port) - - def _create_external_policy(self, consumed_prs, routes=None): - with self.network(router__external=True, shared=True) as net: - with self.subnet(cidr='192.168.0.0/24', network=net) as sub: - if not routes: - routes = [{'destination': '172.0.0.0/22', 'nexthop': None}] - self.create_external_segment( - shared=True, - name="default", - external_routes=routes, - subnet_id=sub['subnet']['id']) - return self.create_external_policy( - consumed_policy_rule_sets={consumed_prs: ''}) - - def _test_lb_node_create(self, consumer_external=False): - with mock.patch.object(heatClient.HeatClient, - 'create') as stack_create: - stack_create.return_value = {'stack': { - 'id': uuidutils.generate_uuid()}} - - node_id = self._create_profiled_servicechain_node( - service_type=constants.LOADBALANCERV2)[ - 'servicechain_node']['id'] - spec = self.create_servicechain_spec( - nodes=[node_id], - expected_res_status=201)['servicechain_spec'] - - prs = self._create_redirect_prs(spec['id'])['policy_rule_set'] - provider = self.create_policy_target_group()['policy_target_group'] - - _, port1 = self._create_policy_target_port(provider['id']) - _, port2 = self._create_policy_target_port(provider['id']) - - if consumer_external: - self._create_external_policy(prs['id']) - else: - self.create_policy_target_group( - consumed_policy_rule_sets={prs['id']: ''}) - - self.update_policy_target_group( - provider['id'], provided_policy_rule_sets={prs['id']: ''}) - created_stacks_map = self._get_node_instance_stacks(node_id) - self.assertEqual(1, len(created_stacks_map)) - - pool_member1 = self._get_pool_member_resource_dict(port1) - pool_member2 = self._get_pool_member_resource_dict(port2) - - # Instantiating the chain invokes stack create - expected_stack_template = copy.deepcopy( - self.DEFAULT_LB_CONFIG_DICT) - expected_stack_template['Resources'].update(pool_member1) - expected_stack_template['Resources'].update(pool_member2) - expected_stack_name = mock.ANY - # TODO(Magesh): Verify expected_stack_params with IP address from - # Network Service Policy - expected_stack_params = {} - stack_create.assert_called_once_with( - expected_stack_name, - expected_stack_template, - expected_stack_params) - return (expected_stack_template, provider, - created_stacks_map[0].stack_id) - - def _test_lb_dynamic_pool_member_add(self, expected_stack_template, - provider, stack_id): - with mock.patch.object(heatClient.HeatClient, - 'update') as stack_update: - stack_update.return_value = {'stack': { - 'id': stack_id}} - - # Creating PT will update the node, thereby adding the PT as an - # LB Pool Member using heat stack - pt, port = self._create_policy_target_port(provider['id']) - - pool_member = self._get_pool_member_resource_dict(port) - expected_stack_template['Resources'].update(pool_member) - expected_stack_id = stack_id - expected_stack_params = {} - stack_update.assert_called_once_with( - expected_stack_id, - expected_stack_template, - expected_stack_params) - return (pt, pool_member) - - def _test_dynamic_lb_pool_member_delete(self, pt, pool_member, - expected_stack_template, - stack_id): - # Deleting PT will update the node, thereby removing the Pool - # Member from heat stack - with mock.patch.object(heatClient.HeatClient, - 'update') as stack_update: - self.delete_policy_target(pt['id']) - - template_on_delete_pt = copy.deepcopy(expected_stack_template) - template_on_delete_pt['Resources'].pop(list(pool_member.keys())[0]) - expected_stack_id = stack_id - expected_stack_params = {} - stack_update.assert_called_once_with( - expected_stack_id, - template_on_delete_pt, - expected_stack_params) - - def _test_node_cleanup(self, ptg, stack_id): - with mock.patch.object(heatClient.HeatClient, - 'delete') as stack_delete: - self.update_policy_target_group( - ptg['id'], consumed_policy_rule_sets={}, - expected_res_status=200) - self.delete_policy_target_group(ptg['id'], expected_res_status=204) - stack_delete.assert_called_once_with(stack_id) - - def test_lb_node_operations(self): - expected_stack_template, provider, stack_id = ( - self._test_lb_node_create()) - pt, pool_member = self._test_lb_dynamic_pool_member_add( - expected_stack_template, provider, stack_id) - self._test_dynamic_lb_pool_member_delete( - pt, pool_member, expected_stack_template, stack_id) - self._test_node_cleanup(provider, stack_id) - - def test_lb_redirect_from_external(self): - expected_stack_template, provider, stack_id = ( - self._test_lb_node_create(consumer_external=True)) - pt, pool_member = self._test_lb_dynamic_pool_member_add( - expected_stack_template, provider, stack_id) - self._test_dynamic_lb_pool_member_delete( - pt, pool_member, expected_stack_template, stack_id) - self._test_node_cleanup(provider, stack_id) - - def _create_fwredirect_ruleset(self, classifier_port, classifier_protocol): - node_id = self._create_profiled_servicechain_node( - service_type=constants.FIREWALL)['servicechain_node']['id'] - spec = self.create_servicechain_spec( - nodes=[node_id], - expected_res_status=201)['servicechain_spec'] - action = self.create_policy_action(action_type='REDIRECT', - action_value=spec['id']) - classifier = self.create_policy_classifier( - port_range=classifier_port, protocol=classifier_protocol, - direction='bi') - rule = self.create_policy_rule( - policy_actions=[action['policy_action']['id']], - policy_classifier_id=classifier['policy_classifier']['id']) - rule = rule['policy_rule'] - prs = self.create_policy_rule_set(policy_rules=[rule['id']]) - return (prs['policy_rule_set'], node_id) - - def _get_ptg_cidr(self, ptg): - req = self.new_show_request( - 'subnets', ptg['subnets'][0], fmt=self.fmt) - ptg_subnet = self.deserialize( - self.fmt, req.get_response(self.api))['subnet'] - return ptg_subnet['cidr'] - - def _get_firewall_rule_dict(self, rule_name, protocol, port, provider_cidr, - consumer_cidr): - if provider_cidr and consumer_cidr: - fw_rule = {rule_name: {'type': "OS::Neutron::FirewallRule", - 'properties': { - "protocol": protocol, - "enabled": True, - "destination_port": port, - "action": "allow", - "destination_ip_address": provider_cidr, - "source_ip_address": consumer_cidr - } - } - } - return fw_rule - return {} - - def test_fw_node_east_west(self): - classifier_port = '66' - classifier_protocol = 'udp' - with mock.patch.object(heatClient.HeatClient, - 'create') as stack_create: - stack_create.return_value = {'stack': { - 'id': uuidutils.generate_uuid()}} - prs, node_id = self._create_fwredirect_ruleset( - classifier_port, classifier_protocol) - provider = self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''})[ - 'policy_target_group'] - self.create_policy_target_group( - consumed_policy_rule_sets={prs['id']: ''}) - - created_stacks_map = self._get_node_instance_stacks(node_id) - self.assertEqual(1, len(created_stacks_map)) - stack_id = created_stacks_map[0].stack_id - - provider_cidr = self._get_ptg_cidr(provider) - # TODO(ivar): This has to be removed once support to consumer list - # is implemented - # consumer_cidr = self._get_ptg_cidr(consumer) - consumer_cidr = [] - fw_rule = self._get_firewall_rule_dict( - 'Rule_1', classifier_protocol, classifier_port, - provider_cidr, consumer_cidr) - - expected_stack_template = copy.deepcopy( - self.DEFAULT_FW_CONFIG_DICT) - expected_stack_template['resources'][ - 'test_fw_policy']['properties']['firewall_rules'] = [] - expected_stack_template['resources'].update(fw_rule) - expected_stack_name = mock.ANY - expected_stack_params = {} - stack_create.assert_called_once_with( - expected_stack_name, - expected_stack_template, - expected_stack_params) - - self._test_node_cleanup(provider, stack_id) - - def _test_fw_node_north_south(self, consumer_cidrs): - classifier_port = '66' - classifier_protocol = 'udp' - with mock.patch.object(heatClient.HeatClient, - 'create') as stack_create: - stack_create.return_value = {'stack': { - 'id': uuidutils.generate_uuid()}} - prs, node_id = self._create_fwredirect_ruleset( - classifier_port, classifier_protocol) - provider = self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''})[ - 'policy_target_group'] - - routes = [] - for consumer_cidr in consumer_cidrs: - routes.append({'destination': consumer_cidr, 'nexthop': None}) - self._create_external_policy(prs['id'], routes=routes) - - # TODO(ivar): This has to be removed once support to consumer list - # is implemented - consumer_cidrs = [] - - created_stacks_map = self._get_node_instance_stacks(node_id) - self.assertEqual(1, len(created_stacks_map)) - stack_id = created_stacks_map[0].stack_id - - expected_stack_template = copy.deepcopy( - self.DEFAULT_FW_CONFIG_DICT) - expected_stack_template['resources']['test_fw_policy'][ - 'properties']['firewall_rules'] = [] - provider_cidr = self._get_ptg_cidr(provider) - - rule_num = 1 - for consumer_cidr in consumer_cidrs: - rule_name = 'Rule_' + str(rule_num) - fw_rule = self._get_firewall_rule_dict( - rule_name, classifier_protocol, classifier_port, - provider_cidr, consumer_cidr) - rule_num = rule_num + 1 - expected_stack_template['resources'].update(fw_rule) - expected_stack_template['resources']['test_fw_policy'][ - 'properties']['firewall_rules'].append( - {'get_resource': rule_name}) - - expected_stack_name = mock.ANY - expected_stack_params = {} - stack_create.assert_called_once_with( - expected_stack_name, - expected_stack_template, - expected_stack_params) - - self._test_node_cleanup(provider, stack_id) - - def test_fw_node_north_south_single_external_cidr(self): - self._test_fw_node_north_south(['172.0.0.0/22']) - - def test_fw_node_north_south_multiple_external_cidr(self): - self._test_fw_node_north_south(['172.0.0.0/22', '20.0.0.0/16']) - - def test_node_update(self): - with mock.patch.object(heatClient.HeatClient, - 'create') as stack_create: - stack_create.return_value = {'stack': { - 'id': uuidutils.generate_uuid()}} - prof = self.create_service_profile( - service_type=constants.LOADBALANCERV2, - vendor=self.SERVICE_PROFILE_VENDOR)['service_profile'] - - node = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_LB_CONFIG, - expected_res_status=201)['servicechain_node'] - - self._create_chain_with_nodes(node_ids=[node['id']]) - with mock.patch.object(heatClient.HeatClient, - 'update') as stack_update: - self.update_servicechain_node( - node['id'], - name='newname', - expected_res_status=200) - # Name update should not update stack ?? - stack_update.assert_called_once_with( - mock.ANY, mock.ANY, mock.ANY) - - def test_node_delete(self): - with mock.patch.object(heatClient.HeatClient, - 'create') as stack_create: - stack_create.return_value = {'stack': { - 'id': uuidutils.generate_uuid()}} - provider, _, _ = self._create_simple_service_chain() - with mock.patch.object(heatClient.HeatClient, - 'delete'): - self.update_policy_target_group( - provider['id'], - provided_policy_rule_sets={}, - expected_res_status=200) - self.delete_policy_target_group(provider['id'], - expected_res_status=204) - - def test_wait_stack_delete_for_instance_delete(self): - - with mock.patch.object(heatClient.HeatClient, - 'create') as stack_create: - stack_create.return_value = {'stack': { - 'id': uuidutils.generate_uuid()}} - provider, _, _ = self._create_simple_service_chain() - - # Verify that as part of delete service chain instance we call - # get method for heat stack 5 times before giving up if the state - # does not become DELETE_COMPLETE - with mock.patch.object(heatClient.HeatClient, - 'delete') as stack_delete: - with mock.patch.object(heatClient.HeatClient, - 'get') as stack_get: - stack_get.return_value = MockStackObject( - 'DELETE_IN_PROGRESS') - # Removing the PRSs will make the PTG deletable again - self.update_policy_target_group( - provider['id'], - provided_policy_rule_sets={}, - expected_res_status=200) - self.delete_policy_target_group(provider['id'], - expected_res_status=204) - stack_delete.assert_called_once_with(mock.ANY) - - # Create and delete another service chain instance and verify that - # we call get method for heat stack only once if the stack state - # is DELETE_COMPLETE - provider, _, _ = self._create_simple_service_chain() - with mock.patch.object(heatClient.HeatClient, - 'delete') as stack_delete: - with mock.patch.object(heatClient.HeatClient, - 'get') as stack_get: - stack_get.return_value = MockStackObject( - 'DELETE_COMPLETE') - # Removing the PRSs will make the PTG deletable again - self.update_policy_target_group( - provider['id'], - provided_policy_rule_sets={}, - expected_res_status=200) - self.delete_policy_target_group(provider['id'], - expected_res_status=204) - stack_delete.assert_called_once_with(mock.ANY) - - def test_stack_not_found_ignored(self): - mock.patch(heatclient.__name__ + ".client.Client", - new=MockHeatClientDeleteNotFound).start() - - provider, _, _ = self._create_simple_service_chain() - - # Removing the PRSs will make the PTG deletable again - self.update_policy_target_group(provider['id'], - provided_policy_rule_sets={}, - expected_res_status=200) - self.delete_policy_target_group(provider['id'], - expected_res_status=204) diff --git a/gbpservice/neutron/tests/unit/services/servicechain/ncp/test_ncp_plugin.py b/gbpservice/neutron/tests/unit/services/servicechain/ncp/test_ncp_plugin.py deleted file mode 100644 index 14112d0db..000000000 --- a/gbpservice/neutron/tests/unit/services/servicechain/ncp/test_ncp_plugin.py +++ /dev/null @@ -1,899 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or -# implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from unittest import mock - -import webob.exc - -from neutron.common import config -from neutron_lib import context as n_context -from neutron_lib import exceptions as n_exc -from neutron_lib.plugins import constants as pconst -from neutron_lib.plugins import directory -from oslo_config import cfg -from oslo_serialization import jsonutils - -from gbpservice.neutron.services.grouppolicy import config as gpconfig # noqa -from gbpservice.neutron.services.servicechain.plugins.ncp import ( - context as ncp_context) -from gbpservice.neutron.services.servicechain.plugins.ncp import ( - exceptions as exc) -from gbpservice.neutron.services.servicechain.plugins.ncp import ( - plugin as ncp_plugin) -import gbpservice.neutron.services.servicechain.plugins.ncp.config # noqa -from gbpservice.neutron.services.servicechain.plugins.ncp.node_drivers import ( - dummy_driver as dummy_driver) -from gbpservice.neutron.tests.unit.db.grouppolicy import test_group_policy_db -from gbpservice.neutron.tests.unit.services.grouppolicy import ( - test_resource_mapping as test_gp_driver) -from gbpservice.neutron.tests.unit.services.servicechain import ( - base_test_servicechain_plugin as test_base) - - -class ServiceChainNCPTestPlugin(ncp_plugin.NodeCompositionPlugin): - - supported_extension_aliases = ['servicechain'] + ( - test_group_policy_db.UNSUPPORTED_REQUIRED_EXTS) - path_prefix = "/servicechain" - - -SC_PLUGIN_KLASS = (ServiceChainNCPTestPlugin.__module__ + '.' + - ServiceChainNCPTestPlugin.__name__) -CORE_PLUGIN = test_gp_driver.CORE_PLUGIN -GP_PLUGIN_KLASS = ( - "gbpservice.neutron.services.grouppolicy.plugin.GroupPolicyPlugin" -) -CHAIN_TENANT_ID = 'sci_owner' - - -class NodeCompositionPluginTestMixin(object): - - DEFAULT_LB_CONFIG = '{}' - SERVICE_PROFILE_VENDOR = 'dummy' - - @property - def sc_plugin(self): - return directory.get_plugin(pconst.SERVICECHAIN) - - def _create_service_profile(self, **kwargs): - """Create service profile wrapper that can be used by drivers.""" - return self.create_service_profile(**kwargs) - - def _create_redirect_rule(self, spec_id): - action = self.create_policy_action(action_type='REDIRECT', - action_value=spec_id) - classifier = self.create_policy_classifier( - port_range=80, protocol='tcp', direction='bi') - rule = self.create_policy_rule( - policy_actions=[action['policy_action']['id']], - policy_classifier_id=classifier['policy_classifier']['id']) - return rule - - def _create_redirect_prs(self, spec_id): - rule = self._create_redirect_rule(spec_id)['policy_rule'] - prs = self.create_policy_rule_set(policy_rules=[rule['id']]) - return prs - - def _create_simple_service_chain(self, number_of_nodes=1, - service_type='LOADBALANCERV2'): - prof = self.create_service_profile( - service_type=service_type, - vendor=self.SERVICE_PROFILE_VENDOR)['service_profile'] - - node_ids = [] - for x in range(number_of_nodes): - node_ids.append(self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_LB_CONFIG, - expected_res_status=201)['servicechain_node']['id']) - - return self._create_chain_with_nodes(node_ids) - - def _create_chain_with_nodes(self, node_ids=None): - node_ids = node_ids or [] - spec = self.create_servicechain_spec( - nodes=node_ids, - expected_res_status=201)['servicechain_spec'] - prs = self._create_redirect_prs(spec['id'])['policy_rule_set'] - provider = self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''})['policy_target_group'] - consumer = self.create_policy_target_group( - consumed_policy_rule_sets={prs['id']: ''})['policy_target_group'] - return provider, consumer, prs - - def _add_node_driver(self, name): - inst = dummy_driver.NoopNodeDriver() - inst.initialize(name) - ext = mock.Mock() - ext.obj = inst - self.sc_plugin.driver_manager.ordered_drivers.append(ext) - self.sc_plugin.driver_manager.drivers[name] = ext - - -class NodeCompositionPluginTestCase( - test_base.BaseTestGroupPolicyPluginGroupResources, - NodeCompositionPluginTestMixin): - - def setUp(self, core_plugin=None, gp_plugin=None, node_drivers=None, - node_plumber=None): - cfg.CONF.set_override( - 'extension_drivers', ['proxy_group'], group='group_policy') - if node_drivers: - cfg.CONF.set_override('node_drivers', node_drivers, - group='node_composition_plugin') - cfg.CONF.set_override('node_plumber', node_plumber or 'dummy_plumber', - group='node_composition_plugin') - config.cfg.CONF.set_override('policy_drivers', - ['implicit_policy', 'resource_mapping', - 'chain_mapping'], - group='group_policy') - super(NodeCompositionPluginTestCase, self).setUp( - core_plugin=core_plugin or CORE_PLUGIN, - gp_plugin=gp_plugin or GP_PLUGIN_KLASS, - sc_plugin=SC_PLUGIN_KLASS) - self.driver = self.sc_plugin.driver_manager.ordered_drivers[0].obj - - def _create_simple_chain(self): - node = self._create_profiled_servicechain_node( - service_type="LOADBALANCERV2", - config=self.DEFAULT_LB_CONFIG)['servicechain_node'] - spec = self.create_servicechain_spec( - nodes=[node['id']])['servicechain_spec'] - - action = self.create_policy_action( - action_type='REDIRECT', action_value=spec['id'])['policy_action'] - classifier = self.create_policy_classifier( - direction='bi', port_range=80, protocol='tcp')['policy_classifier'] - rule = self.create_policy_rule( - policy_classifier_id=classifier['id'], - policy_actions=[action['id']])['policy_rule'] - - prs = self.create_policy_rule_set( - policy_rules=[rule['id']])['policy_rule_set'] - - provider = self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''})['policy_target_group'] - consumer = self.create_policy_target_group( - consumed_policy_rule_sets={prs['id']: ''})['policy_target_group'] - - return provider, consumer, node - - def test_spec_ordering_list_servicechain_instances(self): - pass - - def test_context_attributes(self): - # Verify Context attributes for simple config - plugin_context = n_context.get_admin_context() - profile = self._create_service_profile( - service_type="LOADBALANCERV2", - vendor=self.SERVICE_PROFILE_VENDOR)['service_profile'] - node = self.create_servicechain_node( - service_profile_id=profile['id'], - config=self.DEFAULT_LB_CONFIG)['servicechain_node'] - spec = self.create_servicechain_spec( - nodes=[node['id']])['servicechain_spec'] - provider = self.create_policy_target_group()['policy_target_group'] - self.create_policy_target_group() - management = self.create_policy_target_group( - service_management=True, - is_admin_context=True)['policy_target_group'] - classifier = self.create_policy_classifier()['policy_classifier'] - instance = self.create_servicechain_instance( - provider_ptg_id=provider['id'], consumer_ptg_id='N/A', - servicechain_specs=[spec['id']], classifier_id=classifier['id'])[ - 'servicechain_instance'] - - # Verify created without errors - ctx = ncp_context.get_node_driver_context( - self.plugin, plugin_context, instance, node) - - self.assertIsNotNone(ctx.gbp_plugin) - self.assertIsNotNone(ctx.sc_plugin) - self.assertIsNotNone(ctx.core_plugin) - self.assertIsNotNone(ctx.plugin_context) - self.assertIsNotNone(ctx.plugin_session) - self.assertIsNotNone(ctx.session) - self.assertIsNotNone(ctx.admin_context) - self.assertIsNotNone(ctx.admin_session) - del ctx.current_profile['nodes'] - self.assertEqual(ctx.current_profile['id'], profile['id']) - self.assertEqual(instance['id'], ctx.instance['id']) - self.assertEqual(provider['id'], ctx.provider['id']) - self.assertIsNone(ctx.consumer) - self.assertEqual(management['id'], ctx.management['id']) - self.assertEqual([spec['id']], [x['id'] for x in ctx.relevant_specs]) - self.assertIsNone(ctx.original_node) - self.assertEqual(0, len(ctx.get_service_targets())) - - instance['provider_ptg_id'] = 'dummy-id' - ctx = ncp_context.get_node_driver_context( - self.plugin, plugin_context, instance, node) - self.assertIsNone(ctx.provider) - self.assertIsNone(ctx.consumer) - - def test_context_relevant_specs(self): - plugin_context = n_context.get_admin_context() - node_used = self._create_profiled_servicechain_node( - service_type="LOADBALANCERV2", - config=self.DEFAULT_LB_CONFIG)['servicechain_node'] - spec_used = self.create_servicechain_spec( - nodes=[node_used['id']])['servicechain_spec'] - - provider = self.create_policy_target_group()['policy_target_group'] - classifier = self.create_policy_classifier()['policy_classifier'] - instance = self.create_servicechain_instance( - provider_ptg_id=provider['id'], - classifier_id=classifier['id'], - servicechain_specs=[spec_used['id']])['servicechain_instance'] - - ctx = ncp_context.get_node_driver_context( - self.plugin, plugin_context, instance, node_used) - self.assertEqual([spec_used['id']], - [x['id'] for x in ctx.relevant_specs]) - - def test_manager_initialized(self): - mgr = self.plugin.driver_manager - self.assertIsInstance(mgr.ordered_drivers[0].obj, - dummy_driver.NoopNodeDriver) - for driver in mgr.ordered_drivers: - self.assertTrue(driver.obj.initialized) - - def test_spec_parameters(self): - """Test that config_param_names is empty when using NCP. - In NCP the config attribute of a node may be something different than - a HEAT template, therefore config_param_names is not used. - """ - - params_node_1 = ['p1', 'p2', 'p3'] - params_node_2 = ['p4', 'p5', 'p6'] - params_node_3 = ['p7', 'p8', 'p9'] - - def params_dict(params): - return jsonutils.dumps({'Parameters': - dict((x, {}) for x in params)}) - - prof = self._create_service_profile( - service_type='LOADBALANCERV2', shared=True, - vendor=self.SERVICE_PROFILE_VENDOR, - tenant_id='admin')['service_profile'] - - # Create 2 nodes with different parameters - node1 = self.create_servicechain_node( - service_profile_id=prof['id'], shared=True, - config=params_dict(params_node_1), - expected_res_status=201)['servicechain_node'] - node2 = self.create_servicechain_node( - service_profile_id=prof['id'], shared=True, - config=params_dict(params_node_2), - expected_res_status=201)['servicechain_node'] - - # Create SC spec with the nodes assigned - spec = self.create_servicechain_spec( - nodes=[node1['id'], node2['id']], shared=True, - expected_res_status=201)['servicechain_spec'] - - # Verify param names is empty - self.assertIsNone(spec['config_param_names']) - - # Update the spec removing one node - self.update_servicechain_spec(spec['id'], nodes=[node1['id']], - expected_res_status=200) - - spec = self.show_servicechain_spec(spec['id'])['servicechain_spec'] - # Verify param names is empty - self.assertIsNone(spec['config_param_names']) - - # Update a node with new config params - self.update_servicechain_node(node1['id'], - config=params_dict(params_node_3), - expected_res_status=200) - - spec = self.show_servicechain_spec(spec['id'])['servicechain_spec'] - # Verify param names is empty - self.assertIsNone(spec['config_param_names']) - - def test_create_service_chain(self): - deploy = self.driver.create = mock.Mock() - destroy = self.driver.delete = mock.Mock() - - self._create_simple_service_chain(1) - self.assertEqual(1, deploy.call_count) - self.assertEqual(0, destroy.call_count) - - deploy.reset_mock() - - provider, _, _ = self._create_simple_service_chain(3) - self.assertEqual(3, deploy.call_count) - self.assertEqual(0, destroy.call_count) - - self.update_policy_target_group(provider['id'], - provided_policy_rule_sets={}) - self.assertEqual(3, deploy.call_count) - self.assertEqual(3, destroy.call_count) - - def test_update_service_chain(self): - deploy = self.driver.create = mock.Mock() - update = self.driver.update = mock.Mock() - destroy = self.driver.delete = mock.Mock() - - provider, _, prs = self._create_simple_service_chain(1) - self.assertEqual(1, deploy.call_count) - self.assertEqual(0, destroy.call_count) - - # REVISIT(Magesh): When bug #1446587 is fixed, we should test by - # performing a classifier or rule update instead of SC instance update - instances = self._list('servicechain_instances')[ - 'servicechain_instances'] - self.assertEqual(1, len(instances)) - self.update_servicechain_instance( - instances[0]['id'], - expected_res_status=200) - self.assertEqual(1, update.call_count) - self.assertEqual(0, destroy.call_count) - - def test_create_service_chain_fails(self): - deploy = self.driver.create = mock.Mock() - destroy = self.driver.delete = mock.Mock() - - deploy.side_effect = Exception - - try: - self._create_simple_service_chain(3) - except Exception: - pass - - self.assertEqual(1, deploy.call_count) - self.assertEqual(3, destroy.call_count) - - def test_update_node_fails(self): - validate_update = self.driver.validate_update = mock.Mock() - - prof = self._create_service_profile( - service_type='LOADBALANCERV2', - vendor=self.SERVICE_PROFILE_VENDOR)['service_profile'] - - node_id = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_LB_CONFIG, - expected_res_status=201)['servicechain_node']['id'] - - spec = self.create_servicechain_spec( - nodes=[node_id], - expected_res_status=201)['servicechain_spec'] - prs = self._create_redirect_prs(spec['id'])['policy_rule_set'] - self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''}) - self.create_policy_target_group( - consumed_policy_rule_sets={prs['id']: ''}) - - validate_update.side_effect = exc.NodeCompositionPluginBadRequest( - resource='node', msg='reason') - - res = self.update_servicechain_node(node_id, - description='somethingelse', - expected_res_status=400) - self.assertEqual('NodeCompositionPluginBadRequest', - res['NeutronError']['type']) - - def test_update_instantiated_profile_fails(self): - prof = self._create_service_profile( - service_type='LOADBALANCERV2', - vendor=self.SERVICE_PROFILE_VENDOR)['service_profile'] - - node_id = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_LB_CONFIG, - expected_res_status=201)['servicechain_node']['id'] - - spec = self.create_servicechain_spec( - nodes=[node_id], expected_res_status=201)['servicechain_spec'] - prs = self._create_redirect_prs(spec['id'])['policy_rule_set'] - self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''}) - self.create_policy_target_group( - consumed_policy_rule_sets={prs['id']: ''}) - - res = self.update_service_profile(prof['id'], - vendor='somethingelse', - expected_res_status=400) - self.assertEqual('ServiceProfileInUseByAnInstance', - res['NeutronError']['type']) - - def test_second_driver_scheduled_if_first_fails(self): - self._add_node_driver('test') - drivers = [x.obj for x in - self.sc_plugin.driver_manager.ordered_drivers] - create_1 = drivers[0].validate_create = mock.Mock() - create_1.side_effect = n_exc.NeutronException() - - # This happens without error - profile = self._create_service_profile( - service_type="TYPE", - vendor=self.SERVICE_PROFILE_VENDOR)['service_profile'] - node = self.create_servicechain_node( - service_profile_id=profile['id'], - config=self.DEFAULT_LB_CONFIG)['servicechain_node'] - spec = self.create_servicechain_spec( - nodes=[node['id']])['servicechain_spec'] - provider = self.create_policy_target_group()['policy_target_group'] - classifier = self.create_policy_classifier()['policy_classifier'] - self.create_servicechain_instance( - provider_ptg_id=provider['id'], consumer_ptg_id='N/A', - servicechain_specs=[spec['id']], classifier_id=classifier['id'], - expected_res_status=201) - - def test_chain_fails_if_no_drivers_available(self): - self._add_node_driver('test') - drivers = [x.obj for x in - self.sc_plugin.driver_manager.ordered_drivers] - create_1 = drivers[0].validate_create = mock.Mock() - create_1.side_effect = n_exc.NeutronException() - create_2 = drivers[1].validate_create = mock.Mock() - create_2.side_effect = n_exc.NeutronException() - - profile = self._create_service_profile( - service_type="TYPE", - vendor=self.SERVICE_PROFILE_VENDOR)['service_profile'] - node = self.create_servicechain_node( - service_profile_id=profile['id'], - config=self.DEFAULT_LB_CONFIG)['servicechain_node'] - spec = self.create_servicechain_spec( - nodes=[node['id']])['servicechain_spec'] - provider = self.create_policy_target_group()['policy_target_group'] - classifier = self.create_policy_classifier()['policy_classifier'] - self.create_servicechain_instance( - provider_ptg_id=provider['id'], consumer_ptg_id='N/A', - servicechain_specs=[spec['id']], classifier_id=classifier['id'], - expected_res_status=400) - - def test_multiple_nodes_update(self): - update = self.driver.update = mock.Mock() - prof = self._create_service_profile( - service_type='LOADBALANCERV2', - vendor=self.SERVICE_PROFILE_VENDOR)['service_profile'] - node = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_LB_CONFIG)['servicechain_node'] - - self._create_chain_with_nodes([node['id']]) - self.update_servicechain_node(node['id'], name='somethingelse') - self.assertEqual(1, update.call_count) - - update.reset_mock() - self._create_chain_with_nodes([node['id']]) - self._create_chain_with_nodes([node['id']]) - self.update_servicechain_node(node['id'], name='somethingelse') - self.assertEqual(3, update.call_count) - - def test_inuse_spec_node_update_rejected(self): - prof = self.create_service_profile( - service_type='LOADBALANCERV2', - vendor=self.SERVICE_PROFILE_VENDOR)['service_profile'] - - node1 = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_LB_CONFIG)['servicechain_node'] - node2 = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_LB_CONFIG)['servicechain_node'] - - spec = self.create_servicechain_spec( - nodes=[node1['id'], node2['id']], - expected_res_status=201)['servicechain_spec'] - prs = self._create_redirect_prs(spec['id'])['policy_rule_set'] - self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''}) - self.create_policy_target_group( - consumed_policy_rule_sets={prs['id']: ''}) - - res = self.update_servicechain_spec(spec['id'], - nodes=[node1['id']], - expected_res_status=400) - self.assertEqual('InuseSpecNodeUpdateNotAllowed', - res['NeutronError']['type']) - - def test_instance_update(self): - prof = self.create_service_profile( - service_type='LOADBALANCERV2', - vendor=self.SERVICE_PROFILE_VENDOR)['service_profile'] - - node1 = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_LB_CONFIG)['servicechain_node'] - node2 = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_LB_CONFIG)['servicechain_node'] - - spec = self.create_servicechain_spec( - nodes=[node1['id'], node2['id']], - expected_res_status=201)['servicechain_spec'] - prs = self._create_redirect_prs(spec['id'])['policy_rule_set'] - self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''}) - self.create_policy_target_group( - consumed_policy_rule_sets={prs['id']: ''}) - - instances = self._list('servicechain_instances')[ - 'servicechain_instances'] - self.assertEqual(1, len(instances)) - spec2 = self.create_servicechain_spec( - nodes=[node1['id']], - expected_res_status=201)['servicechain_spec'] - res = self.update_servicechain_instance( - instances[0]['id'], servicechain_specs=[spec2['id']], - expected_res_status=200) - self.assertEqual([spec2['id']], - res['servicechain_instance']['servicechain_specs']) - - def test_relevant_ptg_update(self): - add = self.driver.update_policy_target_added = mock.Mock() - rem = self.driver.update_policy_target_removed = mock.Mock() - - prof = self._create_service_profile( - service_type='LOADBALANCERV2', - vendor=self.SERVICE_PROFILE_VENDOR)['service_profile'] - node = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_LB_CONFIG, - expected_res_status=201)['servicechain_node'] - - spec = self.create_servicechain_spec( - nodes=[node['id']], - expected_res_status=201)['servicechain_spec'] - prs = self._create_redirect_prs(spec['id'])['policy_rule_set'] - provider = self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''})['policy_target_group'] - self.create_policy_target_group( - consumed_policy_rule_sets={prs['id']: ''}) - - # Verify notification issued for created PT in the provider - pt = self.create_policy_target( - policy_target_group_id=provider['id'])['policy_target'] - pt['port_attributes'] = {} - self.assertEqual(1, add.call_count) - add.assert_called_with(mock.ANY, pt) - del pt['port_attributes'] - - # Verify notification issued for deleted PT in the provider - self.delete_policy_target(pt['id']) - self.assertEqual(1, rem.call_count) - rem.assert_called_with(mock.ANY, pt) - - def test_irrelevant_ptg_update(self): - add = self.driver.update_policy_target_added = mock.Mock() - rem = self.driver.update_policy_target_removed = mock.Mock() - - prof = self._create_service_profile( - service_type='LOADBALANCERV2', - vendor=self.SERVICE_PROFILE_VENDOR)['service_profile'] - node = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_LB_CONFIG, - expected_res_status=201)['servicechain_node'] - - spec = self.create_servicechain_spec( - nodes=[node['id']], expected_res_status=201)['servicechain_spec'] - prs = self._create_redirect_prs(spec['id'])['policy_rule_set'] - self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''}) - self.create_policy_target_group( - consumed_policy_rule_sets={prs['id']: ''}) - - other = self.create_policy_target_group()['policy_target_group'] - - # Verify notification issued for created PT in the provider - pt = self.create_policy_target( - policy_target_group_id=other['id'])['policy_target'] - self.assertFalse(add.called) - - # Verify notification issued for deleted PT in the provider - self.delete_policy_target(pt['id']) - self.assertFalse(rem.called) - - def test_notify_chain_update_hook(self): - update_hook = self.driver.notify_chain_parameters_updated = mock.Mock() - - prof = self.create_service_profile( - service_type='LOADBALANCERV2', - vendor=self.SERVICE_PROFILE_VENDOR)['service_profile'] - node = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_LB_CONFIG, - expected_res_status=201)['servicechain_node'] - spec = self.create_servicechain_spec( - nodes=[node['id']], - expected_res_status=201)['servicechain_spec'] - - action = self.create_policy_action(action_type='REDIRECT', - action_value=spec['id']) - classifier = self.create_policy_classifier( - port_range=80, protocol='tcp', direction='bi')['policy_classifier'] - rule = self.create_policy_rule( - policy_actions=[action['policy_action']['id']], - policy_classifier_id=classifier['id'])['policy_rule'] - prs = self.create_policy_rule_set( - policy_rules=[rule['id']])['policy_rule_set'] - - self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''}) - self.create_policy_target_group( - consumed_policy_rule_sets={prs['id']: ''}) - instances = self._list('servicechain_instances')[ - 'servicechain_instances'] - self.assertEqual(1, len(instances)) - - self.update_policy_classifier(classifier['id'], port_range=22) - update_hook.assert_called_with(mock.ANY) - - def test_context_no_management(self): - # Verify Context attributes for simple config - plugin_context = n_context.get_admin_context() - plugin_context.is_admin = False - plugin_context.is_advsvc = False - plugin_context.tenant_id = 'test-tenant' - node = self._create_profiled_servicechain_node()['servicechain_node'] - spec = self.create_servicechain_spec( - nodes=[node['id']])['servicechain_spec'] - provider = self.create_policy_target_group()['policy_target_group'] - - # Verify admin created SM is None - management = self.create_policy_target_group( - service_management=True, tenant_id='admin', - is_admin_context=True)['policy_target_group'] - pc = self.create_policy_classifier()['policy_classifier'] - instance = self.create_servicechain_instance( - provider_ptg_id=provider['id'], consumer_ptg_id='N/A', - servicechain_specs=[spec['id']], - classifier_id=pc['id'])['servicechain_instance'] - ctx = ncp_context.get_node_driver_context( - self.plugin, plugin_context, instance, node) - - self.assertIsNone(ctx.management) - - self.delete_policy_target_group(management['id'], - is_admin_context=True) - shared_management = self.create_policy_target_group( - service_management=True, tenant_id='admin', - is_admin_context=True, shared=True)['policy_target_group'] - instance = self.create_servicechain_instance( - provider_ptg_id=provider['id'], consumer_ptg_id='N/A', - servicechain_specs=[spec['id']], - classifier_id=pc['id'])['servicechain_instance'] - # Now admin Service Management PTG is visible - ctx = ncp_context.get_node_driver_context( - self.plugin, plugin_context, instance, node) - self.assertEqual(shared_management['id'], ctx.management['id']) - - # Private management overrides shared one - private_management = self.create_policy_target_group( - service_management=True, - is_admin_context=True)['policy_target_group'] - instance = self.create_servicechain_instance( - provider_ptg_id=provider['id'], consumer_ptg_id='N/A', - servicechain_specs=[spec['id']], - classifier_id=pc['id'])['servicechain_instance'] - ctx = ncp_context.get_node_driver_context( - self.plugin, plugin_context, instance, node) - self.assertEqual(private_management['id'], ctx.management['id']) - - def test_node_drivers_notified_consumer_event(self): - add = self.driver.update_node_consumer_ptg_added = mock.Mock() - rem = self.driver.update_node_consumer_ptg_removed = mock.Mock() - - prof = self._create_service_profile( - service_type='LOADBALANCERV2', - vendor=self.SERVICE_PROFILE_VENDOR)['service_profile'] - node = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_LB_CONFIG, - expected_res_status=201)['servicechain_node'] - - spec = self.create_servicechain_spec( - nodes=[node['id']], - expected_res_status=201)['servicechain_spec'] - prs = self._create_redirect_prs(spec['id'])['policy_rule_set'] - self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''}) - consumer = self.create_policy_target_group( - consumed_policy_rule_sets={prs['id']: ''})['policy_target_group'] - - # Verify notification issued for PTG consuming - add.assert_called_with(mock.ANY, consumer) - - # Verify notification issued for PTG unconsuming - consumer = self.update_policy_target_group( - consumer['id'], - consumed_policy_rule_sets={})['policy_target_group'] - rem.assert_called_with(mock.ANY, consumer) - - provider, consumer, prs = self._create_simple_service_chain(3) - with mock.patch.object(ncp_plugin.NodeCompositionPlugin, - "update_chains_consumer_removed") as ptg_removed: - plugin_context = n_context.get_admin_context() - self._gbp_plugin.delete_policy_target_group( - plugin_context, consumer['id']) - - self.assertEqual(ptg_removed.call_count, 1) - consumer['consumed_policy_rule_sets'] = [] - ptg_removed.assert_called_once_with( - mock.ANY, consumer, mock.ANY) - - add.reset_mock() - rem.reset_mock() - - def test_no_unrelated_chains_notified(self): - add = self.driver.update_node_consumer_ptg_added = mock.Mock() - rem = self.driver.update_node_consumer_ptg_removed = mock.Mock() - - prof = self._create_service_profile( - service_type='LOADBALANCERV2', - vendor=self.SERVICE_PROFILE_VENDOR)['service_profile'] - node = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_LB_CONFIG, - expected_res_status=201)['servicechain_node'] - - spec = self.create_servicechain_spec( - nodes=[node['id']], - expected_res_status=201)['servicechain_spec'] - prs = self._create_redirect_prs(spec['id'])['policy_rule_set'] - # This creates a chain - self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''}) - - # Create a PRS and assign a consumer with no provider (hence, no chain) - prs = self._create_redirect_prs(spec['id'])['policy_rule_set'] - ptg = self.create_policy_target_group( - consumed_policy_rule_sets={prs['id']: ''})['policy_target_group'] - - # No notification should be issued - self.assertFalse(add.called) - self.assertFalse(rem.called) - - # Remove the consumer - self.update_policy_target_group(ptg['id'], - consumed_policy_rule_sets={}, - expected_res_status=200) - # No notification should be issued - self.assertFalse(add.called) - self.assertFalse(rem.called) - - def test_node_drivers_notified_provider_updated(self): - upd = self.driver.policy_target_group_updated = mock.Mock() - - prof = self._create_service_profile( - service_type='LOADBALANCERV2', - vendor=self.SERVICE_PROFILE_VENDOR)['service_profile'] - node = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_LB_CONFIG, - expected_res_status=201)['servicechain_node'] - - spec = self.create_servicechain_spec( - nodes=[node['id']], - expected_res_status=201)['servicechain_spec'] - prs = self._create_redirect_prs(spec['id'])['policy_rule_set'] - provider = self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''})['policy_target_group'] - - # TODO(Sumit): Remove the following mocks - # once Heat node driver supports reporting status - provider['status'] = mock.ANY - provider['status_details'] = mock.ANY - # Verify notification issued for PTG consuming - upd.assert_called_with(mock.ANY, None, provider) - upd.reset_mock() - - # Verify notification issued for PTG consuming - new_provider = self.update_policy_target_group( - provider['id'], - consumed_policy_rule_sets={prs['id']: ''})['policy_target_group'] - upd.assert_called_with(mock.ANY, provider, new_provider) - - upd.reset_mock() - - -class TestQuotasForServiceChain(test_base.ServiceChainPluginTestCase): - - @property - def sc_plugin(self): - return directory.get_plugin(pconst.SERVICECHAIN) - - def setUp(self, core_plugin=None, gp_plugin=None, node_drivers=None, - node_plumber=None): - if node_drivers: - cfg.CONF.set_override('node_drivers', node_drivers, - group='node_composition_plugin') - cfg.CONF.set_override('node_plumber', node_plumber or 'dummy_plumber', - group='node_composition_plugin') - config.cfg.CONF.set_override('policy_drivers', - ['implicit_policy', 'resource_mapping', - 'chain_mapping'], - group='group_policy') - super(TestQuotasForServiceChain, self).setUp( - core_plugin=core_plugin or CORE_PLUGIN, - gp_plugin=gp_plugin or GP_PLUGIN_KLASS, - sc_plugin=SC_PLUGIN_KLASS) - self.driver = self.sc_plugin.driver_manager.ordered_drivers[0].obj - cfg.CONF.set_override('quota_servicechain_node', 1, - group='QUOTAS') - cfg.CONF.set_override('quota_servicechain_spec', 1, - group='QUOTAS') - cfg.CONF.set_override('quota_servicechain_instance', 1, - group='QUOTAS') - cfg.CONF.set_override('quota_service_profile', 1, - group='QUOTAS') - - def tearDown(self): - cfg.CONF.set_override('quota_servicechain_node', -1, - group='QUOTAS') - cfg.CONF.set_override('quota_servicechain_spec', -1, - group='QUOTAS') - cfg.CONF.set_override('quota_servicechain_instance', -1, - group='QUOTAS') - cfg.CONF.set_override('quota_service_profile', -1, - group='QUOTAS') - super(TestQuotasForServiceChain, self).tearDown() - - def test_servicechain_node_quota(self): - self.create_servicechain_node() - self.assertRaises(webob.exc.HTTPClientError, - self.create_servicechain_node) - - def test_servicechain_spec_quota(self): - self.create_servicechain_spec() - self.assertRaises(webob.exc.HTTPClientError, - self.create_servicechain_spec) - - def test_servicechain_instance_quota(self): - self.create_servicechain_instance() - self.assertRaises(webob.exc.HTTPClientError, - self.create_servicechain_instance) - - def test_service_profile(self): - self.create_service_profile(service_type=pconst.FIREWALL) - self.assertRaises(webob.exc.HTTPClientError, - self.create_service_profile, - service_type=pconst.FIREWALL) - - def test_quota_implicit_service_instance(self): - prof = self.create_service_profile( - service_type='LOADBALANCERV2', - vendor="vendor")['service_profile'] - - node1_id = self.create_servicechain_node( - service_profile_id=prof['id'], config="{}", - expected_res_status=201)['servicechain_node']['id'] - - spec = self.create_servicechain_spec( - nodes=[node1_id], - expected_res_status=201)['servicechain_spec'] - action = self.create_policy_action(action_type='REDIRECT', - action_value=spec['id']) - classifier = self.create_policy_classifier( - port_range=80, protocol='tcp', direction='bi') - rule = self.create_policy_rule( - policy_actions=[action['policy_action']['id']], - policy_classifier_id=classifier['policy_classifier']['id']) - prs = self.create_policy_rule_set( - policy_rules=[rule['policy_rule']['id']])['policy_rule_set'] - self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''}) - self.create_policy_target_group( - consumed_policy_rule_sets={prs['id']: ''}) - # Second service instance creation should fail now - # sice service instance quota is 1, resulting in PTG - # creation error - self.assertRaises(webob.exc.HTTPClientError, - self.create_policy_target_group, - provided_policy_rule_sets={prs['id']: ''}) diff --git a/gbpservice/neutron/tests/unit/services/servicechain/ncp/test_nfp_node_driver.py b/gbpservice/neutron/tests/unit/services/servicechain/ncp/test_nfp_node_driver.py deleted file mode 100644 index 14535ca01..000000000 --- a/gbpservice/neutron/tests/unit/services/servicechain/ncp/test_nfp_node_driver.py +++ /dev/null @@ -1,859 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or -# implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from unittest import mock - -from neutron_lib.plugins import constants -from oslo_serialization import jsonutils -import webob - -from gbpservice.neutron.services.servicechain.plugins.ncp import ( - plugin as ncp_plugin) -from gbpservice.neutron.services.servicechain.plugins.ncp import config # noqa -from gbpservice.neutron.services.servicechain.plugins.ncp.node_drivers import ( - nfp_node_driver as nfp_node_driver) -from gbpservice.neutron.tests.unit.db.grouppolicy import test_group_policy_db -from gbpservice.neutron.tests.unit.services.grouppolicy import ( - test_resource_mapping as test_gp_driver) -from gbpservice.neutron.tests.unit.services.servicechain import ( - base_test_servicechain_plugin as test_base) -from gbpservice.neutron.tests.unit.services.servicechain.ncp import ( - test_ncp_plugin as test_ncp_plugin) -from gbpservice.nfp.orchestrator.db import nfp_db as nfp_db - -SERVICE_DELETE_TIMEOUT = 15 -SVC_MANAGEMENT_PTG = 'foo' - - -class ServiceChainNCPTestPlugin(ncp_plugin.NodeCompositionPlugin): - - supported_extension_aliases = ['servicechain'] + ( - test_group_policy_db.UNSUPPORTED_REQUIRED_EXTS) - path_prefix = "/servicechain" - - -SC_PLUGIN_KLASS = (ServiceChainNCPTestPlugin.__module__ + '.' + - ServiceChainNCPTestPlugin.__name__) -CORE_PLUGIN = test_gp_driver.CORE_PLUGIN -GP_PLUGIN_KLASS = ( - "gbpservice.neutron.services.grouppolicy.plugin.GroupPolicyPlugin" -) - - -class NFPNodeDriverTestCase( - test_base.BaseTestGroupPolicyPluginGroupResources, - test_ncp_plugin.NodeCompositionPluginTestMixin): - - DEFAULT_VPN_CONFIG_DICT = { - "heat_template_version": "2013-05-23", - "description": "Creates new vpn service", - "parameters": { - "RouterId": { - "type": "string", "description": "Router ID" - }, - "Subnet": { - "type": "string", "description": "Subnet id" - }, - "ClientAddressPoolCidr": { - "type": "string", "description": "Pool" - }, - }, - "resources": { - "SSLVPNConnection": { - "type": "OS::Neutron::SSLVPNConnection", - "properties": { - "credential_id": "", - "client_address_pool_cidr": { - "get_param": "ClientAddressPoolCidr" - }, - "name": "vtun0", - "vpnservice_id": { - "get_resource": "VPNService" - }, - "admin_state_up": 'true' - } - }, - "VPNService": { - "type": "OS::Neutron::VPNService", - "properties": { - "router_id": { - "get_param": "RouterId" - }, - "subnet_id": { - "get_param": "Subnet" - }, - "admin_state_up": 'true', - "name": "VPNService" - } - } - } - } - DEFAULT_VPN_CONFIG = jsonutils.dumps(DEFAULT_VPN_CONFIG_DICT) - DEFAULT_LB_CONFIG_DICT = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "test_pool": { - "Type": "OS::Neutron::Pool", - "Properties": { - "admin_state_up": True, - "description": "Haproxy pool from teplate", - "lb_method": "ROUND_ROBIN", - "monitors": [{"Ref": "HttpHM"}], - "name": "Haproxy pool", - "protocol": "HTTP", - "subnet_id": {"Ref": "Subnet"}, - "vip": { - "subnet": {"Ref": "192.168.100.0"}, - "address": {"Ref": "192.168.100.2"}, - "name": "Haproxy vip", - "protocol_port": 80, - "connection_limit": -1, - "admin_state_up": True, - "description": "Haproxy vip from template" - } - } - }, - "test_lb": { - "Type": "OS::Neutron::LoadBalancer", - "Properties": { - "pool_id": {"Ref": "HaproxyPool"}, - "protocol_port": 80 - } - } - } - } - DEFAULT_LB_CONFIG = jsonutils.dumps(DEFAULT_LB_CONFIG_DICT) - DEFAULT_FW_CONFIG_DICT = { - "heat_template_version": "2013-05-23", - "resources": { - 'test_fw': { - "type": "OS::Neutron::Firewall", - "properties": { - "admin_state_up": True, - "firewall_policy_id": { - "get_resource": "Firewall_policy"}, - "name": "testFirewall", - "description": "test Firewall" - } - }, - 'test_fw_policy': { - "type": "OS::Neutron::FirewallPolicy", - "properties": { - "shared": False, - "description": "test firewall policy", - "name": "testFWPolicy", - "firewall_rules": [{ - "get_resource": "Rule_1"}], - "audited": True - } - } - } - } - DEFAULT_FW_CONFIG = jsonutils.dumps(DEFAULT_FW_CONFIG_DICT) - SERVICE_PROFILE_VENDOR = 'NFP' - - def _create_service_profile(self, **kwargs): - if not kwargs.get('insertion_mode'): - kwargs['insertion_mode'] = 'l3' - if not kwargs.get('service_flavor'): - if kwargs['service_type'] == 'LOADBALANCERV2': - kwargs['service_flavor'] = 'haproxy' - else: - kwargs['service_flavor'] = 'vyos' - return super(NFPNodeDriverTestCase, self)._create_service_profile( - **kwargs) - - def setUp(self): - config.cfg.CONF.set_override('service_delete_timeout', - SERVICE_DELETE_TIMEOUT, - group='nfp_node_driver') - - config.cfg.CONF.set_override( - 'extension_drivers', ['proxy_group'], group='group_policy') - config.cfg.CONF.set_override('node_drivers', ['nfp_node_driver'], - group='node_composition_plugin') - config.cfg.CONF.set_override('node_plumber', 'stitching_plumber', - group='node_composition_plugin') - config.cfg.CONF.set_override('policy_drivers', - ['implicit_policy', 'resource_mapping', - 'chain_mapping'], - group='group_policy') - super(NFPNodeDriverTestCase, self).setUp( - core_plugin=CORE_PLUGIN, - gp_plugin=GP_PLUGIN_KLASS, - sc_plugin=SC_PLUGIN_KLASS) - - def test_manager_initialized(self): - mgr = self.plugin.driver_manager - self.assertIsInstance(mgr.ordered_drivers[0].obj, - nfp_node_driver.NFPNodeDriver) - for driver in mgr.ordered_drivers: - self.assertTrue(driver.obj.initialized) - - def _nfp_create_profiled_servicechain_node( - self, service_type=constants.LOADBALANCERV2, shared_profile=False, - profile_tenant_id=None, profile_id=None, - service_flavor=None, **kwargs): - if not profile_id: - prof = self.create_service_profile( - service_type=service_type, - shared=shared_profile, - vendor=self.SERVICE_PROFILE_VENDOR, - insertion_mode='l3', service_flavor='haproxy', - tenant_id=profile_tenant_id or self._tenant_id)[ - 'service_profile'] - else: - prof = self.get_service_profile(profile_id) - service_config = kwargs.get('config') - if not service_config or service_config == '{}': - if service_type == constants.FIREWALL: - kwargs['config'] = self.DEFAULT_FW_CONFIG - else: - kwargs['config'] = self.DEFAULT_LB_CONFIG - return self.create_servicechain_node( - service_profile_id=prof['id'], **kwargs) - - def _create_simple_fw_service_chain(self, number_of_nodes=1, - service_type='FIREWALL'): - prof = self.create_service_profile( - service_type=service_type, - vendor=self.SERVICE_PROFILE_VENDOR, - insertion_mode='l3', service_flavor='vyos')['service_profile'] - node_ids = [] - for x in range(number_of_nodes): - node_ids.append(self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_FW_CONFIG, - expected_res_status=201)['servicechain_node']['id']) - return self._nfp_create_chain_with_nodes(node_ids) - - def _nfp_create_chain_with_nodes(self, node_ids=None): - node_ids = node_ids or [] - spec = self.create_servicechain_spec( - nodes=node_ids, - expected_res_status=201)['servicechain_spec'] - prs = self._create_redirect_prs(spec['id'])['policy_rule_set'] - provider = self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''})['policy_target_group'] - with mock.patch.object(nfp_node_driver.NFPClientApi, - "consumer_ptg_added_notification") as ptg_added: - consumer = self.create_policy_target_group( - consumed_policy_rule_sets={prs['id']: ''})[ - 'policy_target_group'] - ptg_added.assert_called_once_with(mock.ANY, - mock.ANY, mock.ANY) - return provider, consumer, prs - - def test_spec_parameters(self): - pass - - def test_spec_ordering_list_servicechain_instances(self): - pass - - -class DummyMap(object): - network_function_id = '12' - status = 'UP' - - -class TestServiceChainInstance(NFPNodeDriverTestCase): - - @mock.patch.object(nfp_node_driver.NFPClientApi, 'get_plumbing_info') - def test_node_create(self, plumbing_info): - with mock.patch.object(nfp_node_driver.NFPClientApi, - "create_network_function") as create_nf: - with mock.patch.object(nfp_node_driver.NFPClientApi, - "get_network_function") as get_nf: - create_nf.return_value = { - 'id': '126231632163' - } - get_nf.return_value = { - 'id': '126231632163', - 'status': 'ACTIVE' - } - plumbing_info.return_value = { - 'management': [], - 'provider': [{}], - 'consumer': [{}], - 'plumbing_type': 'gateway' - } - self._create_simple_fw_service_chain() - create_nf.assert_called_once_with(mock.ANY, mock.ANY) - - def _test_node_update(self): - with mock.patch.object(nfp_node_driver.NFPClientApi, - "create_network_function") as create_nf: - with mock.patch.object(nfp_node_driver.NFPClientApi, - "get_network_function") as get_nf: - with mock.patch.object(nfp_node_driver.NFPClientApi, - "update_service_config") as update_svc_config: - create_nf.return_value = { - 'id': '126231632163' - } - get_nf.return_value = { - 'id': '126231632163', - 'status': 'ACTIVE' - } - prof = self.create_service_profile( - service_type=constants.FIREWALL, - vendor=self.SERVICE_PROFILE_VENDOR, - insertion_mode='l3', - service_flavor='vyos')['service_profile'] - - self.create_policy_target_group( - name='foo')['policy_target_group'] - node = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_FW_CONFIG, - expected_res_status=201)['servicechain_node'] - - self._nfp_create_chain_with_nodes(node_ids=[node['id']]) - self.update_servicechain_node( - node['id'], - name='newname', - expected_res_status=200) - create_nf.assert_called_once_with(mock.ANY, mock.ANY) - update_svc_config.assert_called_once_with() - - @mock.patch.object(nfp_node_driver.NFPClientApi, 'get_plumbing_info') - def test_node_delete(self, plumbing_info): - with mock.patch.object(nfp_node_driver.NFPClientApi, - "create_network_function") as create_nf: - with mock.patch.object(nfp_node_driver.NFPClientApi, - 'get_network_function') as get_nf: - get_nf.return_value = { - 'id': '126231632163', - 'status': 'ACTIVE' - } - create_nf.return_value = { - 'id': '126231632163' - } - - plumbing_info.return_value = { - 'management': [], - 'provider': [{}], - 'consumer': [{}], - 'plumbing_type': 'gateway' - } - prof = self.create_service_profile( - service_type=constants.FIREWALL, - vendor=self.SERVICE_PROFILE_VENDOR, - insertion_mode='l3', - service_flavor='vyos')['service_profile'] - node_id = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_FW_CONFIG, - expected_res_status=201)['servicechain_node'][ - 'id'] - - spec = self.create_servicechain_spec( - nodes=[node_id], - expected_res_status=201)['servicechain_spec'] - prs = self._create_redirect_prs(spec['id'])['policy_rule_set'] - provider = self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''})[ - 'policy_target_group'] - create_nf.assert_called_once_with(mock.ANY, mock.ANY) - with mock.patch.object(nfp_node_driver.NFPClientApi, - "get_network_function") as get_nf: - with mock.patch.object(nfp_node_driver.NFPClientApi, - "delete_network_function") as delete_nf,\ - mock.patch.object(nfp_db.NFPDbBase, - "get_node_instance_network_function_map") as get_map,\ - mock.patch.object(nfp_db.NFPDbBase, - "update_node_instance_network_function_map") as update_map: - - get_map.return_value = DummyMap() - update_map.return_value = mock.ANY - get_nf.return_value = None - self.delete_policy_target_group( - provider['id'], expected_res_status=204) - delete_nf.assert_called_once_with(mock.ANY, mock.ANY, mock.ANY) - - @mock.patch.object(nfp_node_driver.NFPClientApi, 'get_plumbing_info') - def test_wait_for_network_function_delete_completion(self, plumbing_info): - with mock.patch.object(nfp_node_driver.NFPClientApi, - "create_network_function") as create_nf: - with mock.patch.object(nfp_node_driver.NFPClientApi, - 'get_network_function') as get_nf: - get_nf.return_value = { - 'id': '126231632163', - 'status': 'ACTIVE' - } - create_nf.return_value = { - 'id': '126231632163' - } - plumbing_info.return_value = { - 'management': [], - 'provider': [{}], - 'consumer': [{}], - 'plumbing_type': 'gateway' - } - prof = self.create_service_profile( - service_type=constants.FIREWALL, - vendor=self.SERVICE_PROFILE_VENDOR, - insertion_mode='l3', - service_flavor='vyos')['service_profile'] - node_id = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_FW_CONFIG, - expected_res_status=201)['servicechain_node'][ - 'id'] - - spec = self.create_servicechain_spec( - nodes=[node_id], - expected_res_status=201)['servicechain_spec'] - prs = self._create_redirect_prs(spec['id'])['policy_rule_set'] - provider = self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''})[ - 'policy_target_group'] - create_nf.assert_called_once_with(mock.ANY, mock.ANY) - - with mock.patch.object(nfp_node_driver.NFPClientApi, - 'delete_network_function') as delete_nf: - with mock.patch.object(nfp_node_driver.NFPClientApi, - 'get_network_function') as get_nf,\ - mock.patch.object(nfp_db.NFPDbBase, - "get_node_instance_network_function_map") as get_map,\ - mock.patch.object(nfp_db.NFPDbBase, - "update_node_instance_network_function_map") as \ - update_map: - - get_map.return_value = DummyMap() - update_map.return_value = mock.ANY - delete_nf.return_value = None - get_nf.return_value = None - # Removing the PRSs will make the PTG deletable again - self.update_policy_target_group( - provider['id'], - provided_policy_rule_sets={}, - expected_res_status=200) - self.delete_policy_target_group(provider['id'], - expected_res_status=204) - delete_nf.assert_called_once_with(mock.ANY, mock.ANY, - mock.ANY) - - def _create_policy_target_port(self, policy_target_group_id): - pt = self.create_policy_target( - policy_target_group_id=policy_target_group_id)['policy_target'] - req = self.new_show_request('ports', pt['port_id'], fmt=self.fmt) - port = self.deserialize(self.fmt, - req.get_response(self.api))['port'] - return (pt, port) - - @mock.patch.object(nfp_node_driver.NFPClientApi, 'get_plumbing_info') - def test_lb_node_create(self, plumbing_info, consumer_external=False): - with mock.patch.object(nfp_node_driver.NFPClientApi, - "create_network_function") as create_nf: - with mock.patch.object(nfp_node_driver.NFPClientApi, - 'get_network_function') as get_nf: - get_nf.return_value = { - 'id': '126231632163', - 'status': 'ACTIVE' - } - create_nf.return_value = { - 'id': '126231632163' - } - plumbing_info.return_value = { - 'management': [], - 'provider': [{}], - 'consumer': [{}], - 'plumbing_type': 'endpoint' - } - node_id = self._nfp_create_profiled_servicechain_node( - service_type=constants.LOADBALANCERV2)[ - 'servicechain_node']['id'] - spec = self.create_servicechain_spec( - nodes=[node_id], - expected_res_status=201)['servicechain_spec'] - - prs = self._create_redirect_prs(spec['id'])['policy_rule_set'] - params = [{'type': 'ip_single', 'name': 'vip_ip', - 'value': 'self_subnet'}] - - nsp = self.create_network_service_policy( - network_service_params=params) - network_service_policy_id = nsp['network_service_policy']['id'] - provider = self.create_policy_target_group( - network_service_policy_id=network_service_policy_id, - provided_policy_rule_sets={prs['id']: ''})[ - 'policy_target_group'] - - with mock.patch.object(nfp_node_driver.NFPClientApi, - "policy_target_added_notification") as pt_added: - # Verify notification issued for created PT in the provider - _, port = self._create_policy_target_port(provider['id']) - pt_added.assert_called_once_with(mock.ANY, mock.ANY, - mock.ANY) - - if consumer_external: - self._create_external_policy(prs['id']) - else: - self.create_policy_target_group( - consumed_policy_rule_sets={prs['id']: ''}) - - create_nf.assert_called_once_with(mock.ANY, mock.ANY) - - def test_invalid_service_type_rejected(self): - node_used = self._nfp_create_profiled_servicechain_node( - service_type="test")['servicechain_node'] - spec_used = self.create_servicechain_spec( - nodes=[node_used['id']])['servicechain_spec'] - provider = self.create_policy_target_group()['policy_target_group'] - classifier = self.create_policy_classifier()['policy_classifier'] - res = self.create_servicechain_instance( - provider_ptg_id=provider['id'], - classifier_id=classifier['id'], - servicechain_specs=[spec_used['id']], - expected_res_status=webob.exc.HTTPBadRequest.code) - self.assertEqual('NoDriverAvailableForAction', - res['NeutronError']['type']) - - def test_is_node_order_in_spec_supported(self): - lb_prof = self.create_service_profile( - service_type=constants.LOADBALANCERV2, - vendor=self.SERVICE_PROFILE_VENDOR, - insertion_mode='l3', - service_flavor='haproxy')['service_profile'] - vpn_prof = self.create_service_profile( - service_type=constants.VPN, - vendor=self.SERVICE_PROFILE_VENDOR, - insertion_mode='l3', - service_flavor='vyos')['service_profile'] - vpn_node = self.create_servicechain_node( - service_profile_id=vpn_prof['id'], - config=self.DEFAULT_VPN_CONFIG, - expected_res_status=201)['servicechain_node'] - lb_node = self.create_servicechain_node( - service_profile_id=lb_prof['id'], - config=self.DEFAULT_LB_CONFIG, - expected_res_status=201)['servicechain_node'] - node_ids = [lb_node['id'], vpn_node['id']] - spec = self.create_servicechain_spec( - nodes=node_ids, - expected_res_status=201)['servicechain_spec'] - - provider = self.create_policy_target_group()['policy_target_group'] - classifier = self.create_policy_classifier()['policy_classifier'] - res = self.create_servicechain_instance( - provider_ptg_id=provider['id'], - classifier_id=classifier['id'], - servicechain_specs=[spec['id']], - expected_res_status=webob.exc.HTTPBadRequest.code) - self.assertEqual('NoDriverAvailableForAction', - res['NeutronError']['type']) - - @mock.patch.object(nfp_node_driver.NFPClientApi, 'get_plumbing_info') - def test_validate_update(self, plumbing_info): - with mock.patch.object(nfp_node_driver.NFPClientApi, - "create_network_function") as create_nf: - with mock.patch.object(nfp_node_driver.NFPClientApi, - "get_network_function") as get_nf: - create_nf.return_value = { - 'id': '126231632163' - } - get_nf.return_value = { - 'id': '126231632163', - 'status': 'ACTIVE' - } - plumbing_info.return_value = { - 'management': [], - 'provider': [{}], - 'consumer': [{}], - 'plumbing_type': 'gateway' - } - fw_prof = self.create_service_profile( - service_type=constants.FIREWALL, - vendor=self.SERVICE_PROFILE_VENDOR, - insertion_mode='l3', - service_flavor='vyos')['service_profile'] - fw_node = self.create_servicechain_node( - service_profile_id=fw_prof['id'], - config=self.DEFAULT_FW_CONFIG, - expected_res_status=201)['servicechain_node'] - node_ids = [fw_node['id']] - spec = self.create_servicechain_spec( - nodes=node_ids, - expected_res_status=201)['servicechain_spec'] - provider = self.create_policy_target_group()[ - 'policy_target_group'] - classifier = self.create_policy_classifier()[ - 'policy_classifier'] - servicechain_instance = self.create_servicechain_instance( - provider_ptg_id=provider['id'], - classifier_id=classifier['id'], - servicechain_specs=[spec['id']])[ - 'servicechain_instance'] - fw_prof = self.create_service_profile( - service_type='test', - vendor=self.SERVICE_PROFILE_VENDOR, - insertion_mode='l3', - service_flavor='vyos')['service_profile'] - fw_node = self.create_servicechain_node( - service_profile_id=fw_prof['id'], - config=self.DEFAULT_FW_CONFIG, - expected_res_status=201)['servicechain_node'] - node_ids = [fw_node['id']] - spec = self.create_servicechain_spec( - nodes=node_ids, - expected_res_status=201)['servicechain_spec'] - create_nf.assert_called_once_with(mock.ANY, mock.ANY) - with mock.patch.object(nfp_node_driver.NFPClientApi, - "get_network_function") as get_nf: - with mock.patch.object(nfp_node_driver.NFPClientApi, - "delete_network_function") as delete_nf,\ - mock.patch.object(nfp_db.NFPDbBase, - "get_node_instance_network_function_map") as get_map,\ - mock.patch.object(nfp_db.NFPDbBase, - "update_node_instance_network_function_map") as \ - update_map: - get_map.return_value = DummyMap() - update_map.return_value = mock.ANY - get_nf.return_value = None - res = self.update_servicechain_instance( - servicechain_instance['id'], - servicechain_specs=[spec['id']], - expected_res_status=webob.exc.HTTPBadRequest.code) - delete_nf.assert_called_once_with(mock.ANY, - mock.ANY, mock.ANY) - self.assertEqual('NoDriverAvailableForAction', - res['NeutronError']['type']) - - @mock.patch.object(nfp_node_driver.NFPClientApi, 'get_plumbing_info') - def test_update_node_consumer_ptg_added(self, plumbing_info): - with mock.patch.object(nfp_node_driver.NFPClientApi, - "create_network_function") as create_nf: - with mock.patch.object(nfp_node_driver.NFPClientApi, - 'get_network_function') as get_nf: - get_nf.return_value = { - 'id': '126231632163', - 'status': 'ACTIVE' - } - create_nf.return_value = { - 'id': '126231632163' - } - plumbing_info.return_value = { - 'management': [], - 'provider': [{}], - 'consumer': [{}], - 'plumbing_type': 'gateway' - } - prof = self.create_service_profile( - service_type=constants.FIREWALL, - vendor=self.SERVICE_PROFILE_VENDOR, - insertion_mode='l3', - service_flavor='vyos')['service_profile'] - node_id = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_FW_CONFIG, - expected_res_status=201)['servicechain_node'][ - 'id'] - - spec = self.create_servicechain_spec( - nodes=[node_id], - expected_res_status=201)['servicechain_spec'] - prs = self._create_redirect_prs(spec['id'])['policy_rule_set'] - self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''})[ - 'policy_target_group'] - create_nf.assert_called_once_with(mock.ANY, mock.ANY) - with mock.patch.object(nfp_node_driver.NFPClientApi, - "consumer_ptg_added_notification") as ptg_added: - self.create_policy_target_group( - consumed_policy_rule_sets={prs['id']: ''})[ - 'policy_target_group'] - ptg_added.assert_called_once_with(mock.ANY, - mock.ANY, mock.ANY) - - def _test_update_node_consumer_ptg_removed(self): - with mock.patch.object(nfp_node_driver.NFPClientApi, - "create_network_function") as create_nf: - with mock.patch.object(nfp_node_driver.NFPClientApi, - 'get_network_function') as get_nf: - get_nf.return_value = { - 'id': '126231632163', - 'status': 'ACTIVE' - } - create_nf.return_value = { - 'id': '126231632163' - } - - prof = self.create_service_profile( - service_type=constants.FIREWALL, - vendor=self.SERVICE_PROFILE_VENDOR, - insertion_mode='l3', - service_flavor='vyos')['service_profile'] - node_id = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_FW_CONFIG, - expected_res_status=201)['servicechain_node'][ - 'id'] - - spec = self.create_servicechain_spec( - nodes=[node_id], - expected_res_status=201)['servicechain_spec'] - prs = self._create_redirect_prs(spec['id'])['policy_rule_set'] - self.create_policy_target_group( - provided_policy_rule_sets={prs['id']: ''})[ - 'policy_target_group'] - with mock.patch.object(nfp_node_driver.NFPClientApi, - "consumer_ptg_added_notification") as ptg_added: - consumer = self.create_policy_target_group( - consumed_policy_rule_sets={prs['id']: ''})[ - 'policy_target_group'] - ptg_added.assert_called_once_with(mock.ANY, mock.ANY, - mock.ANY) - create_nf.assert_called_once_with(mock.ANY, mock.ANY) - - with mock.patch.object(nfp_node_driver.NFPClientApi, - "consumer_ptg_removed_notification") as ptg_removed: - self.delete_policy_target_group( - consumer['id'], expected_res_status=204) - ptg_removed.assert_called_once_with(mock.ANY, mock.ANY, mock.ANY) - - @mock.patch.object(nfp_node_driver.NFPClientApi, 'get_plumbing_info') - def test_policy_target_add_remove(self, plumbing_info): - prof = self._create_service_profile( - service_type='LOADBALANCERV2', - vendor=self.SERVICE_PROFILE_VENDOR, - insertion_mode='l3', service_flavor='haproxy')['service_profile'] - node = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_LB_CONFIG, - expected_res_status=201)['servicechain_node'] - - spec = self.create_servicechain_spec( - nodes=[node['id']], - expected_res_status=201)['servicechain_spec'] - prs = self._create_redirect_prs(spec['id'])['policy_rule_set'] - with mock.patch.object(nfp_node_driver.NFPClientApi, - "create_network_function") as create_nf: - with mock.patch.object(nfp_node_driver.NFPClientApi, - 'get_network_function') as get_nf: - get_nf.return_value = { - 'id': '126231632163', - 'status': 'ACTIVE' - } - create_nf.return_value = { - 'id': '126231632163' - } - plumbing_info.return_value = { - 'management': [], - 'provider': [{}], - 'consumer': [{}], - 'plumbing_type': 'endpoint' - } - params = [{'type': 'ip_single', 'name': 'vip_ip', - 'value': 'self_subnet'}] - nsp = self.create_network_service_policy( - network_service_params=params) - network_service_policy_id = nsp['network_service_policy'][ - 'id'] - provider = self.create_policy_target_group( - network_service_policy_id=network_service_policy_id, - provided_policy_rule_sets={prs['id']: ''})[ - 'policy_target_group'] - self.create_policy_target_group( - consumed_policy_rule_sets={prs['id']: ''}) - - with mock.patch.object(nfp_node_driver.NFPClientApi, - "policy_target_added_notification") as pt_added: - # Verify notification issued for created PT in the provider - pt = self.create_policy_target( - policy_target_group_id=provider['id'])[ - 'policy_target'] - create_nf.assert_called_once_with(mock.ANY, mock.ANY) - pt_added.assert_called_once_with(mock.ANY, mock.ANY, - mock.ANY) - - # Verify notification issued for deleted PT in the provider - with mock.patch.object(nfp_node_driver.NFPClientApi, - "policy_target_removed_notification") as pt_removed: - with mock.patch.object(nfp_node_driver.NFPClientApi, - 'get_network_function') as get_nf: - get_nf.return_value = { - 'id': '126231632163', - 'status': 'ACTIVE' - } - self.delete_policy_target(pt['id']) - pt_removed.assert_called_once_with(mock.ANY, mock.ANY, - mock.ANY) - - @mock.patch.object(nfp_node_driver.NFPClientApi, 'get_plumbing_info') - def test_policy_target_group_updated(self, plumbing_info): - prof = self._create_service_profile( - service_type='FIREWALL', - vendor=self.SERVICE_PROFILE_VENDOR, - insertion_mode='l3', service_flavor='vyos')['service_profile'] - node = self.create_servicechain_node( - service_profile_id=prof['id'], - config=self.DEFAULT_FW_CONFIG, - expected_res_status=201)['servicechain_node'] - spec = self.create_servicechain_spec( - nodes=[node['id']])['servicechain_spec'] - - action = self.create_policy_action( - action_type='REDIRECT', action_value=spec['id'])[ - 'policy_action'] - classifier = self.create_policy_classifier( - direction='bi', protocol='icmp')[ - 'policy_classifier'] - rule = self.create_policy_rule( - policy_classifier_id=classifier['id'], - policy_actions=[action['id']])['policy_rule'] - prs = self.create_policy_rule_set( - policy_rules=[rule['id']])['policy_rule_set'] - # allow - allow_action = self.create_policy_action(action_type='ALLOW')[ - 'policy_action'] - allow_rule = self.create_policy_rule( - policy_classifier_id=classifier['id'], - policy_actions=[allow_action['id']])['policy_rule'] - allow_prs = self.create_policy_rule_set( - policy_rules=[allow_rule['id']])['policy_rule_set'] - # ref ptg - ref_ptg = self.create_policy_target_group()['policy_target_group'] - ref_pt = self.create_policy_target( - policy_target_group_id=ref_ptg['id'])['policy_target'] - - with mock.patch.object(nfp_node_driver.NFPClientApi, - "create_network_function") as create_nf: - with mock.patch.object(nfp_node_driver.NFPClientApi, - 'get_network_function') as get_nf: - get_nf.return_value = { - 'id': '126231632163', - 'status': 'ACTIVE' - } - create_nf.return_value = { - 'id': '126231632163' - } - plumbing_info.return_value = { - 'management': [], - 'provider': [{}], - 'consumer': [{}], - 'plumbing_type': 'gateway' - } - orig_ptg = self.create_policy_target_group( - description="opflex_eoc:%s" % ref_pt['port_id'], - provided_policy_rule_sets={prs['id']: ''})[ - 'policy_target_group'] - current_ptg = self.update_policy_target_group( - orig_ptg['id'], - provided_policy_rule_sets={ - prs['id']: '', allow_prs['id']: ''})[ - 'policy_target_group'] - ref_ptg = self.show_policy_target_group(ref_ptg['id'])[ - 'policy_target_group'] - self.assertSetEqual(set(ref_ptg['provided_policy_rule_sets']), - set(current_ptg[ - 'provided_policy_rule_sets'])) diff --git a/gbpservice/neutron/tests/unit/services/servicechain/ncp/test_traffic_stitching_plumber.py b/gbpservice/neutron/tests/unit/services/servicechain/ncp/test_traffic_stitching_plumber.py deleted file mode 100644 index d702c3aa1..000000000 --- a/gbpservice/neutron/tests/unit/services/servicechain/ncp/test_traffic_stitching_plumber.py +++ /dev/null @@ -1,168 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from unittest import mock - -from neutron.common import config # noqa -from neutron_lib import context as n_context -from neutron_lib.plugins import constants as pconst -from oslo_config import cfg - -from gbpservice.neutron.services.servicechain.plugins.ncp import model -from gbpservice.neutron.tests.unit.services.grouppolicy import ( - test_resource_mapping as test_gp_driver) -from gbpservice.neutron.tests.unit.services.servicechain.ncp import ( - test_ncp_plugin as base) - - -class TrafficStitchingPlumberTestCase(base.NodeCompositionPluginTestCase): - - def setUp(self): - cfg.CONF.set_override('policy_drivers', ['implicit_policy', - 'resource_mapping'], - group='group_policy') - cfg.CONF.set_override('allow_overlapping_ips', True) - cfg.CONF.set_override( - 'extension_drivers', ['proxy_group'], group='group_policy') - super(TrafficStitchingPlumberTestCase, self).setUp( - node_drivers=['node_dummy'], node_plumber='stitching_plumber', - core_plugin=test_gp_driver.CORE_PLUGIN) - self.driver = self.sc_plugin.driver_manager.ordered_drivers[0].obj - self.driver.get_plumbing_info = mock.Mock() - self.driver.get_plumbing_info.return_value = {} - - def test_one_gateway_pt_prov_cons(self): - context = n_context.get_admin_context() - self.driver.get_plumbing_info.return_value = { - 'provider': [{}], 'consumer': [{}], 'plumbing_type': 'gateway'} - provider, consumer, node = self._create_simple_chain() - provider = self.show_policy_target_group( - provider['id'])['policy_target_group'] - # Verify Service PT created and correctly placed - targets = model.get_service_targets(context.session) - self.assertEqual(2, len(targets)) - old_relationship = None - for target in targets: - self.assertEqual(node['id'], target.servicechain_node_id) - pt = self.show_policy_target( - target.policy_target_id)['policy_target'] - if target.relationship == 'provider': - self.assertEqual(provider['id'], - pt['policy_target_group_id']) - self.assertTrue(pt['group_default_gateway']) - self.assertFalse(pt['proxy_gateway']) - else: - # Consumer side a proxy group exists - self.assertEqual(provider['proxy_group_id'], - pt['policy_target_group_id']) - self.assertFalse(pt['group_default_gateway']) - self.assertTrue(pt['proxy_gateway']) - - self.assertNotEqual(old_relationship, target.relationship) - old_relationship = target.relationship - port = self._get_object('ports', pt['port_id'], self.api)['port'] - self.assertTrue(port['name'].startswith('pt_service_target_'), - "Port name doesn't start with 'pt_service_target_" - "'.\nport:\n%s\n" % port) - - self.update_policy_target_group( - provider['id'], provided_policy_rule_sets={}) - # With chain deletion, also the Service PTs are deleted - new_targets = model.get_service_targets(context.session) - self.assertEqual(0, len(new_targets)) - for target in targets: - self.show_policy_target( - target.policy_target_id, expected_res_status=404) - provider = self.show_policy_target_group( - provider['id'])['policy_target_group'] - self.assertIsNone(provider['proxy_group_id']) - - def test_multiple_endpoint_pt_provider(self): - context = n_context.get_admin_context() - self.driver.get_plumbing_info.return_value = { - 'provider': [{}, {}], 'consumer': [], 'plumbing_type': 'endpoint'} - provider, consumer, node = self._create_simple_chain() - provider = self.show_policy_target_group( - provider['id'])['policy_target_group'] - # Verify Service PT created and contains proper name, description - targets = model.get_service_targets(context.session) - self.assertEqual(2, len(targets)) - for target in targets: - pt = self.show_policy_target( - target.policy_target_id)['policy_target'] - self.assertEqual(provider['id'], - pt['policy_target_group_id']) - self.assertTrue(pt['name'].startswith('tscp_endpoint_service'), - "Policy Target name doesn't start with " - "'tscp_endpoint_service'.\npt:\n%s\n" % pt) - self.assertTrue(node['id'] in pt['description'], - "Policy Target description doesn't contains " - " node id.\nnode:\n%s\n" % node) - - port = self._get_object('ports', pt['port_id'], self.api)['port'] - self.assertTrue(port['name'].startswith( - 'pt_tscp_endpoint_service'), - "Port name doesn't start with " - "'pt_tscp_endpoint_service'.\nport:\n%s\n" % port) - - self.update_policy_target_group( - provider['id'], provided_policy_rule_sets={}) - # With chain deletion, also the Service PTs are deleted - new_targets = model.get_service_targets(context.session) - self.assertEqual(0, len(new_targets)) - for target in targets: - self.show_policy_target( - target.policy_target_id, expected_res_status=404) - provider = self.show_policy_target_group( - provider['id'])['policy_target_group'] - self.assertIsNone(provider['proxy_group_id']) - - def get_plumbing_info_base(self, context): - service_type = context.current_profile['service_type'] - plumbing_request = {'management': [], 'provider': [{}], - 'consumer': [{}]} - - if service_type in [pconst.FIREWALL]: - plumbing_request['plumbing_type'] = 'gateway' - else: - plumbing_request = {} - return plumbing_request - - def test_get_service_targets_in_chain(self): - context = n_context.get_admin_context() - self.driver.get_plumbing_info = self.get_plumbing_info_base - lb_prof = self._create_service_profile( - service_type='LOADBALANCERV2', - vendor=self.SERVICE_PROFILE_VENDOR)['service_profile'] - lb_node = self.create_servicechain_node( - service_profile_id=lb_prof['id'], - config=self.DEFAULT_LB_CONFIG)['servicechain_node'] - fw_prof = self._create_service_profile( - service_type='FIREWALL', - vendor=self.SERVICE_PROFILE_VENDOR)['service_profile'] - fw_node = self.create_servicechain_node( - service_profile_id=fw_prof['id'], - config='{}')['servicechain_node'] - - self._create_chain_with_nodes([fw_node['id'], lb_node['id']]) - - targets = model.get_service_targets(context.session) - self.assertEqual(2, len(targets)) - - def test_ptg_delete(self): - self.driver.get_plumbing_info.return_value = { - 'provider': [{}], 'consumer': [{}], - 'plumbing_type': 'transparent'} - provider, _, _ = self._create_simple_service_chain() - # Deleting a PTG will fail because of existing PTs - self.delete_policy_target_group(provider['id'], - expected_res_status=204) diff --git a/gbpservice/neutron/tests/unit/services/servicechain/ncp/test_tscp_resource_mapping.py b/gbpservice/neutron/tests/unit/services/servicechain/ncp/test_tscp_resource_mapping.py deleted file mode 100644 index d9e621821..000000000 --- a/gbpservice/neutron/tests/unit/services/servicechain/ncp/test_tscp_resource_mapping.py +++ /dev/null @@ -1,203 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from unittest import mock - -from neutron.common import config # noqa -from neutron_lib import context as n_context -from neutron_lib.plugins import directory -from oslo_config import cfg - -from gbpservice.neutron.services.grouppolicy import ( - policy_driver_manager as pdm) -from gbpservice.neutron.services.servicechain.plugins.ncp import model -from gbpservice.neutron.tests.unit.services.grouppolicy import ( - test_resource_mapping as test_gp_driver) -from gbpservice.neutron.tests.unit.services.servicechain.ncp import ( - test_ncp_plugin as base) - - -GATEWAY = 'gateway' -GATEWAY_HA = 'gateway_ha' -TRANSPARENT = 'transparent' -ENDPOINT = 'endpoint' - -info_mapping = { - GATEWAY: {'plumbing_type': GATEWAY, 'provider': [{}], 'consumer': [{}]}, - GATEWAY_HA: {'plumbing_type': GATEWAY, 'provider': [{}, {}, {}], - 'consumer': [{}, {}, {}]}, - TRANSPARENT: {'plumbing_type': TRANSPARENT, 'provider': [{}], - 'consumer': [{}]}, - ENDPOINT: {'plumbing_type': ENDPOINT, 'provider': [{}]}, - -} -info_mapping['FIREWALL'] = info_mapping[GATEWAY] -info_mapping['FIREWALL_HA'] = info_mapping[GATEWAY_HA] -info_mapping['TRANSPARENT_FIREWALL'] = info_mapping[TRANSPARENT] -info_mapping['LOADBALANCERV2'] = info_mapping[ENDPOINT] - - -class ResourceMappingStitchingPlumberGBPTestCase( - test_gp_driver.ResourceMappingTestCase): - - def setUp(self): - cfg.CONF.set_override( - 'extension_drivers', ['proxy_group'], group='group_policy') - cfg.CONF.set_override('node_plumber', 'stitching_plumber', - group='node_composition_plugin') - ml2_opts = {'mechanism_drivers': ['stitching_gbp'], - 'extension_drivers': ['qos']} - host_agents = mock.patch('neutron.plugins.ml2.driver_context.' - 'PortContext.host_agents').start() - host_agents.return_value = [self.agent_conf] - qos_plugin = 'qos' - super(ResourceMappingStitchingPlumberGBPTestCase, self).setUp( - sc_plugin=base.SC_PLUGIN_KLASS, ml2_options=ml2_opts, - qos_plugin=qos_plugin) - - def get_plumbing_info(context): - return info_mapping.get(context.current_profile['service_type']) - - self.node_driver = self.sc_plugin.driver_manager.ordered_drivers[0].obj - self.node_driver.get_plumbing_info = get_plumbing_info - pdm.PolicyDriverManager.get_policy_target_group_status = ( - mock.MagicMock({})) - - @property - def sc_plugin(self): - return directory.get_plugin('SERVICECHAIN') - - -class TestPolicyRuleSet(ResourceMappingStitchingPlumberGBPTestCase, - test_gp_driver.TestPolicyRuleSet): - pass - - -class TestServiceChain(ResourceMappingStitchingPlumberGBPTestCase, - test_gp_driver.TestServiceChain): - - def test_parent_ruleset_update_for_redirect(self): - # NCP doesn't support multiple SPECs per instance - pass - - def test_enforce_parent_redirect_after_ptg_create(self): - # NCP doesn't support multiple SPECs per instance - pass - - def test_hierarchical_redirect(self): - # NCP doesn't support multiple SPECs per instance - pass - - def test_redirect_multiple_ptgs_single_prs(self): - # REVISIT(ivar): This test is doing a mock patching that breaks the - # workflow - pass - - def test_action_spec_value_update(self): - # NCP doesn't support multiple SPECs per instance - pass - - def test_rule_update_hierarchial_prs(self): - # NCP doesn't support multiple SPECs per instance - pass - - def test_rule_update_updates_chain(self): - # NCP doesn't support multiple SPECs per instance - pass - - -class TestServiceChainAdminOwner(ResourceMappingStitchingPlumberGBPTestCase, - test_gp_driver.TestServiceChainAdminOwner): - - def test_parent_ruleset_update_for_redirect(self): - # NCP doesn't support multiple SPECs per instance - pass - - def test_enforce_parent_redirect_after_ptg_create(self): - # NCP doesn't support multiple SPECs per instance - pass - - def test_hierarchical_redirect(self): - # NCP doesn't support multiple SPECs per instance - pass - - def test_redirect_multiple_ptgs_single_prs(self): - # REVISIT(ivar): This test is doing a mock patching that breaks the - # workflow - pass - - def test_action_spec_value_update(self): - # NCP doesn't support multiple SPECs per instance - pass - - def test_rule_update_hierarchial_prs(self): - # NCP doesn't support multiple SPECs per instance - pass - - def test_rule_update_updates_chain(self): - # NCP doesn't support multiple SPECs per instance - pass - - -class TestPolicyAction(ResourceMappingStitchingPlumberGBPTestCase, - test_gp_driver.TestPolicyAction): - pass - - -class TestPolicyRule(ResourceMappingStitchingPlumberGBPTestCase, - test_gp_driver.TestPolicyRule): - pass - - -class TestExternalSegment(ResourceMappingStitchingPlumberGBPTestCase, - test_gp_driver.TestExternalSegment): - def test_update(self): - super(TestExternalSegment, self).test_update( - proxy_ip_pool1='182.169.0.0/16', - proxy_ip_pool2='172.169.0.0/16') - - -class TestExternalPolicy(ResourceMappingStitchingPlumberGBPTestCase, - test_gp_driver.TestExternalPolicy): - pass - - -class TestImplicitServiceChains(ResourceMappingStitchingPlumberGBPTestCase, - base.NodeCompositionPluginTestMixin): - - def test_service_targets_vif_details(self): - context = n_context.get_admin_context() - self._create_simple_service_chain(service_type='TRANSPARENT_FIREWALL') - targets = model.get_service_targets(context.session) - self.assertGreater(len(targets), 0) - for target in targets: - pt = self.show_policy_target( - target.policy_target_id)['policy_target'] - # Being service targets, port filter and hybrid plug will be false - port = self._bind_port_to_host(pt['port_id'], 'host')['port'] - self.assertFalse(port['binding:vif_details']['port_filter']) - self.assertFalse(port['binding:vif_details']['ovs_hybrid_plug']) - - def test_endpoint_target_vif_details(self): - context = n_context.get_admin_context() - self._create_simple_service_chain(service_type='LOADBALANCERV2') - targets = model.get_service_targets(context.session) - self.assertGreater(len(targets), 0) - for target in targets: - pt = self.show_policy_target( - target.policy_target_id)['policy_target'] - port = self._bind_port_to_host(pt['port_id'], 'host')['port'] - self.assertTrue(port['binding:vif_details']['port_filter']) - # This change sets hybrid VIF plugging to True by default again - # https://github.com/openstack/neutron/commit/ - # eca893be5b770c41cfc570dc016a41c30c2cdf23 - self.assertTrue(port['binding:vif_details']['ovs_hybrid_plug']) diff --git a/gbpservice/nfp/base_configurator/controllers/__init__.py b/gbpservice/nfp/base_configurator/controllers/__init__.py deleted file mode 100644 index cded74e18..000000000 --- a/gbpservice/nfp/base_configurator/controllers/__init__.py +++ /dev/null @@ -1,58 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import pecan - -from gbpservice.nfp.base_configurator.controllers import controller - - -class ControllerResolver(object): - - """This class forwards HTTP request to controller class. - - This class create an object of Controller class with appropriate - parameter according to the path of HTTP request. According to the - parameter passed to Controller class it sends an RPC call/cast to - configurator. - - """ - create_network_function_device_config = controller.Controller( - "create_network_function_device_config") - delete_network_function_device_config = controller.Controller( - "delete_network_function_device_config") - update_network_function_device_config = controller.Controller( - "update_network_function_device_config") - create_network_function_config = controller.Controller( - "create_network_function_config") - delete_network_function_config = controller.Controller( - "delete_network_function_config") - update_network_function_config = controller.Controller( - "update_network_function_config") - get_notifications = controller.Controller("get_notifications") - - -class V1Controller(object): - """ This class forwards HTTP requests starting with /v1/nfp. - - All HTTP requests with path starting from /v1 - land here. This class forward request with path starting from /v1/nfp - to ControllerResolver. - - """ - - nfp = ControllerResolver() - - @pecan.expose() - def get(self): - return {'versions': [{'status': 'CURRENT', - 'updated': '2014-12-11T00:00:00Z', - 'id': 'v1'}]} diff --git a/gbpservice/nfp/base_configurator/controllers/controller.py b/gbpservice/nfp/base_configurator/controllers/controller.py deleted file mode 100644 index b897be427..000000000 --- a/gbpservice/nfp/base_configurator/controllers/controller.py +++ /dev/null @@ -1,219 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import subprocess -from subprocess import CalledProcessError -import time - -from oslo_log import log as logging -import oslo_serialization.jsonutils as jsonutils -import pecan -import requests - -from gbpservice._i18n import _ -from gbpservice.nfp.pecan import base_controller - -LOG = logging.getLogger(__name__) -TOPIC = 'configurator' -NFP_SERVICE_LIST = ['heat', 'ansible'] -SUCCESS_RESULTS = ['unhandled', 'success'] -FAILURE = 'failure' - - -notifications = [] -cache_ips = set() - - -class Controller(base_controller.BaseController): - - """Implements all the APIs Invoked by HTTP requests. - - Implements following HTTP methods. - -get - -post - - """ - def __init__(self, method_name): - try: - self.method_name = method_name - super(Controller, self).__init__() - except Exception as err: - msg = ( - "Failed to initialize Controller class %s." % - str(err).capitalize()) - LOG.error(msg) - self.vm_port = '8080' - self.max_retries = 60 - - def _push_notification(self, context, result, config_data, service_type): - global notifications - resource = config_data['resource'] - - if result.lower() in SUCCESS_RESULTS: - data = {'status_code': result} - else: - data = {'status_code': FAILURE, - 'error_msg': result} - - response = {'info': {'service_type': service_type, - 'context': context}, - 'notification': [{ - 'resource': resource, - 'data': data}] - } - - notifications.append(response) - - def _verify_vm_reachability(self, vm_ip, vm_port): - reachable = False - command = 'nc ' + vm_ip + ' ' + vm_port + ' -z' - ping_command = 'ping -c1 ' + vm_ip - for x in range(self.max_retries): - try: - subprocess.check_output(ping_command, stderr=subprocess.STDOUT, - shell=True) - subprocess.check_output(command, stderr=subprocess.STDOUT, - shell=True) - reachable = True - break - except CalledProcessError as err: - msg = ("Exception: %s " % err) - LOG.error(msg) - time.sleep(5) - except Exception: - time.sleep(5) - return reachable - - @pecan.expose(method='GET', content_type='application/json') - def get(self): - """Method of REST server to handle request get_notifications. - - This method send an RPC call to configurator and returns Notification - data to config-agent - - Returns: Dictionary that contains Notification data - - """ - global cache_ips - global notifications - try: - if not cache_ips: - notification_data = jsonutils.dumps(notifications) - msg = ("Notification sent. Notification Data: %s" - % notification_data) - LOG.info(msg) - notifications = [] - return notification_data - else: - for ip in cache_ips: - notification_response = requests.get( - 'http://' + str(ip) + ':' + self.vm_port + - '/v1/nfp/get_notifications') - notification = jsonutils.loads(notification_response.text) - notifications.extend(notification) - cache_ips.remove(ip) - if ip not in cache_ips: - break - notification_data = jsonutils.dumps(notifications) - msg = ("Notification sent. Notification Data: %s" - % notification_data) - LOG.info(msg) - notifications = [] - - return notification_data - except Exception as err: - pecan.response.status = 400 - msg = ("Failed to get notification_data %s." - % str(err).capitalize()) - LOG.error(msg) - error_data = self._format_description(msg) - return jsonutils.dumps(error_data) - - @pecan.expose(method='POST', content_type='application/json') - def post(self, **body): - """Method of REST server to handle all the post requests. - - This method sends an RPC cast to configurator according to the - HTTP request. - - :param body: This method excepts dictionary as a parameter in HTTP - request and send this dictionary to configurator with RPC cast. - - Returns: None - - """ - try: - global cache_ips - global notifications - body = None - if pecan.request.is_body_readable: - body = pecan.request.json_body - - # Assuming config list will have only one element - config_data = body['config'][0] - info_data = body['info'] - - context = info_data['context'] - service_type = info_data['service_type'] - resource = config_data['resource'] - operation = context['operation'] - - msg1 = ("Request recieved :: %s" % body) - LOG.info(msg1) - if 'device_ip' in context: - msg3 = ("POSTING DATA TO VM :: %s" % body) - LOG.info(msg3) - device_ip = context['device_ip'] - ip = str(device_ip) - if operation == 'delete': - return - msg5 = ("Verifying vm reachability on ip: %s, port: %s" % ( - ip, self.vm_port)) - LOG.info(msg5) - is_vm_reachable = self._verify_vm_reachability(ip, - self.vm_port) - if is_vm_reachable: - requests.post( - 'http://' + ip + ':' + self.vm_port + '/v1/nfp/' + - self.method_name, data=jsonutils.dumps(body)) - msg4 = ("requests successfull for data: %s" % body) - LOG.info(msg4) - else: - raise Exception(_('VM is not reachable')) - cache_ips.add(device_ip) - else: - if (resource in NFP_SERVICE_LIST): - result = "unhandled" - self._push_notification(context, - result, config_data, service_type) - else: - result = "Unsupported resource type" - self._push_notification(context, - result, config_data, service_type) - except Exception as err: - pecan.response.status = 400 - msg = ("Failed to serve HTTP post request %s %s." - % (self.method_name, str(err).capitalize())) - LOG.error(msg) - error_data = self._format_description(msg) - return jsonutils.dumps(error_data) - - def _format_description(self, msg): - """This methgod formats error description. - - :param msg: An error message that is to be formatted - - Returns: error_data dictionary - """ - - error_data = {'failure_desc': {'msg': msg}} - return error_data diff --git a/gbpservice/nfp/common/data_formatter.py b/gbpservice/nfp/common/data_formatter.py deleted file mode 100644 index 504b838d9..000000000 --- a/gbpservice/nfp/common/data_formatter.py +++ /dev/null @@ -1,154 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import copy - -from gbpservice.nfp.common import constants as const - -''' The generic data format that is common for device and -service configuration. - -''' - -NFP_DATA_FORMAT = { - 'config': [{ - 'resource': '', - 'resource_data': { - 'tenant_id': '', - 'nfds': [{ - 'role': 'master', - 'svc_mgmt_fixed_ip': '', - 'networks': [{ - 'type': '', - 'cidr': '', - 'gw_ip': '', - 'ports': [{ - 'fixed_ip': '', - 'floating_ip': '', - 'mac': ''}] # ports - }] # networks - }] # nfds - } # resource_data - }] # config - } # NFP_DATA_FORMAT - - -def _fill_service_specific_info(nfd, device_data, **kwargs): - ''' Service specific data formatting is done here. - - :param nfd: A partly built nested dict from NFP_DATA_FORMAT - :param device_data: Device data dictionary - :param kwargs: service specific arguments - - Returns: nfd dict - - ''' - - network_schema = kwargs.get('network_schema') - resource_type = kwargs.get('resource_type') - provider_network = nfd['networks'][0] - provider_port = provider_network['ports'][0] - - if resource_type == const.FIREWALL: - nfd['svc_mgmt_fixed_ip'] = device_data.get('vm_management_ip') - provider_port['mac'] = device_data.get('provider_ptg_info')[0] - elif resource_type == const.VPN: - stitching_network = nfd['networks'][1] - stitching_port = stitching_network['ports'][0] - nfd['svc_mgmt_fixed_ip'] = device_data.get('fip') - provider_network['cidr'] = device_data.get('tunnel_local_cidr') - stitching_port['fixed_ip'] = device_data.get('fixed_ip') - stitching_port['floating_ip'] = device_data.get('user_access_ip') - stitching_network['cidr'] = device_data.get('stitching_cidr') - stitching_network['gw_ip'] = device_data.get('stitching_gateway') - management_network = copy.deepcopy(network_schema) - management_network['type'] = const.MANAGEMENT - management_network['gw_ip'] = device_data.get('mgmt_gw_ip') - nfd['networks'].append(management_network) - elif resource_type == const.LOADBALANCERV2: - nfd['svc_mgmt_fixed_ip'] = device_data.get('floating_ip') - provider_port['mac'] = device_data.get('provider_interface_mac') - return nfd - - -def get_network_function_info(device_data, resource_type): - ''' Returns a generic configuration format for both device - and service configuration. - - :param device_data: Data to be formatted. Type: dict - :param resource_type: (healthmonitor/device_config/firewall/ - vpn/loadbalancer/loadbalancerv2) - - Return: dictionary - - ''' - - SERVICE_TYPES = [const.FIREWALL, const.VPN, - const.LOADBALANCERV2] - config = copy.deepcopy(NFP_DATA_FORMAT) - - mgmt_ip = device_data.get('mgmt_ip_address') - tenant_id = device_data.get('tenant_id') - provider_ip = device_data.get('provider_ip') - provider_mac = device_data.get('provider_mac') - provider_cidr = device_data.get('provider_cidr') - stitching_ip = device_data.get('consumer_ip') - stitching_mac = device_data.get('consumer_mac') - stitching_cidr = device_data.get('consumer_cidr') - stitching_gateway_ip = device_data.get('consumer_gateway_ip') - - resource_data = config['config'][0]['resource_data'] - resource_data['tenant_id'] = tenant_id - - nfd = resource_data['nfds'][0] - nfd['role'] = 'master' - nfd['svc_mgmt_fixed_ip'] = mgmt_ip - - if resource_type == const.HEALTHMONITOR_RESOURCE: - nfd['periodicity'] = device_data.get('periodicity') - nfd['periodic_polling_reason'] = const.DEVICE_TO_BECOME_DOWN - nfd['vmid'] = device_data['id'] - config['config'][0]['resource'] = const.HEALTHMONITOR_RESOURCE - return config - - provider_network = nfd['networks'][0] - network_schema = copy.deepcopy(provider_network) - provider_network['type'] = const.PROVIDER - provider_network['cidr'] = provider_cidr - provider_network['gw_ip'] = '' - stitching_network = copy.deepcopy(network_schema) - stitching_network['type'] = const.STITCHING - stitching_network['cidr'] = stitching_cidr - stitching_network['gw_ip'] = stitching_gateway_ip - nfd['networks'].append(stitching_network) - - provider_port = provider_network['ports'][0] - provider_port['fixed_ip'] = provider_ip - provider_port['floating_ip'] = '' - provider_port['mac'] = provider_mac - stitching_port = stitching_network['ports'][0] - stitching_port['fixed_ip'] = stitching_ip - stitching_port['floating_ip'] = '' - stitching_port['mac'] = stitching_mac - - if resource_type in SERVICE_TYPES: - nfd = _fill_service_specific_info(nfd, device_data, - network_schema=network_schema, - resource_type=resource_type) - resource_data['nfs'] = resource_data.pop('nfds') - return config['config'][0]['resource_data'] - - config['config'][0]['resource'] = const.INTERFACE_RESOURCE - config['config'].append(config['config'][0].copy()) - config['config'][1]['resource'] = const.ROUTES_RESOURCE - - return config diff --git a/gbpservice/nfp/common/exceptions.py b/gbpservice/nfp/common/exceptions.py deleted file mode 100644 index 59c66524b..000000000 --- a/gbpservice/nfp/common/exceptions.py +++ /dev/null @@ -1,137 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import sys - -from oslo_config import cfg -from oslo_log import log as logging -import six - -from gbpservice._i18n import _ - -LOG = logging.getLogger(__name__) - -exc_log_opts = [ - cfg.BoolOpt('fatal_exception_format_errors', - default=False, - help='Make exception message format errors fatal.'), -] - -CONF = cfg.CONF -CONF.register_opts(exc_log_opts) - - -class NFPException(Exception): - """Base NFP Exception - - To correctly use this class, inherit from it and define - a 'message' property. That message will get printf'd - with the keyword arguments provided to the constructor. - - """ - message = _("An unknown exception occurred.") - code = 500 - headers = {} - safe = False - - def __init__(self, message=None, **kwargs): - self.kwargs = kwargs - self.kwargs['message'] = message - - if 'code' not in self.kwargs: - try: - self.kwargs['code'] = self.code - except AttributeError: - pass - - for k, v in list(self.kwargs.items()): - if isinstance(v, Exception): - self.kwargs[k] = six.text_type(v) - - if self._should_format(): - try: - message = self.message % kwargs - - except Exception: - exc_info = sys.exc_info() - # kwargs doesn't match a variable in the message - # log the issue and the kwargs - LOG.exception('Exception in string format operation') - for name, value in list(kwargs.items()): - LOG.error("%(name)s: %(value)s", - {'name': name, 'value': value}) - if CONF.fatal_exception_format_errors: - six.reraise(*exc_info) - # at least get the core message out if something happened - message = self.message - elif isinstance(message, Exception): - message = six.text_type(message) - - self.msg = message - super(NFPException, self).__init__(message) - - def _should_format(self): - return self.kwargs['message'] is None or '%(message)' in self.message - - def __unicode__(self): - return six.text_type(self.msg) - - -class NotFound(NFPException): - message = _("Resource could not be found.") - code = 404 - safe = True - - -class NetworkFunctionNotFound(NotFound): - message = _("NetworkFunction %(network_function_id)s could not be found") - - -class NetworkFunctionInstanceNotFound(NotFound): - message = _("NetworkFunctionInstance %(network_function_instance_id)s " - "could not be found") - - -class NetworkFunctionDeviceNotFound(NotFound): - message = _("NetworkFunctionDevice %(network_function_device_id)s could " - "not be found") - - -class NetworkFunctionDeviceInterfaceNotFound(NotFound): - message = _("NetworkFunctionDeviceInterface " - "%(network_function_device_interface_id)s could " - "not be found") - - -class NFPPortNotFound(NotFound): - message = _("NFP Port %(port_id)s could not be found") - - -class RequiredDataNotProvided(NFPException): - message = _("The required data %(required_data)s is missing in " - "%(request)s") - - -class IncompleteData(NFPException): - message = _("Data passed is incomplete") - - -class NotSupported(NFPException): - message = _("Feature is not supported") - - -class ComputePolicyNotSupported(NotSupported): - message = _("Compute policy %(compute_policy)s is not supported") - - -class HotplugNotSupported(NotSupported): - message = _("Vendor %(vendor)s doesn't support hotplug feature") diff --git a/gbpservice/nfp/common/topics.py b/gbpservice/nfp/common/topics.py deleted file mode 100644 index 97b73e942..000000000 --- a/gbpservice/nfp/common/topics.py +++ /dev/null @@ -1,17 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -NFP_NSO_TOPIC = "nfp-service-orchestrator" -NFP_NODE_DRIVER_CALLBACK_TOPIC = "nfp-node-driver-callbacks" -NFP_NDO_CONFIGURATOR_TOPIC = "nfp-ndo-configurator" -NFP_CONFIGURATOR_NDO_TOPIC = "nfp-ndo-notification-topic" -NFP_NSO_CONFIGURATOR_TOPIC = "nfp-nso-notification-topic" diff --git a/gbpservice/nfp/common/utils.py b/gbpservice/nfp/common/utils.py deleted file mode 100644 index e9563a3e6..000000000 --- a/gbpservice/nfp/common/utils.py +++ /dev/null @@ -1,122 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import os - -import six -import yaml - -from gbpservice.nfp.common import constants as nfp_constants -from gbpservice.nfp.core import log as nfp_logging - -LOG = nfp_logging.getLogger(__name__) - -NEUTRON_ML2_CONF = "/etc/neutron/plugins/ml2/ml2_conf.ini" - - -def _parse_service_flavor_string(service_flavor_str): - service_details = {} - if ',' not in service_flavor_str: - service_details['device_type'] = 'nova' - service_details['service_vendor'] = service_flavor_str - else: - service_flavor_dict = dict(item.split('=') for item - in service_flavor_str.split(',')) - service_details = {key.strip(): value.strip() for key, value - in six.iteritems(service_flavor_dict)} - return service_details - - -def _get_dict_desc_from_string(vpn_svc): - svc_desc = vpn_svc.split(";") - desc = {} - for ele in svc_desc: - s_ele = ele.split("=") - desc.update({s_ele[0]: s_ele[1]}) - return desc - - -def get_vpn_description_from_nf(network_function): - str_description = network_function['description'].split('\n')[1] - description = _get_dict_desc_from_string( - str_description) - return description, str_description - - -def is_vpn_in_service_chain(sc_specs): - for spec in sc_specs: - nodes = spec['sc_nodes'] - for node in nodes: - service_type = node['sc_service_profile']['service_type'] - if service_type.lower() == nfp_constants.VPN: - return True - return False - - -def get_config_file(service_vendor): - file_name = service_vendor + '.day0' - return file_name - - -def get_service_vm_context(service_vendor, tenant_name=None): - """ Load day0 config file - :param service_vendor: service vendor name - :param tenant_name - - - Day0 file name must start with service vendor name followed by - string '.day0' - e.g Vyos day0 file name must be vyos.day0 - - File format can be of any type like text file, json file etc - - - service vendor specific default day0 config file - /etc/nfp// - e.g /etc/nfp/vyos/vyos.day0 - - tenant specific vendor day0 config file - /etc/nfp/// - e.g /etc/nfp/vyos/services/vyos.day0 - - Returns - day0 config file - """ - try: - file_name = '' - default_config_dir = nfp_constants.CONFIG_DIR - vendor_day0_dir = default_config_dir + service_vendor + '/' - if tenant_name: - tenant_day0_dir = vendor_day0_dir + tenant_name + '/' - if os.path.isdir(tenant_day0_dir): - file_name = get_config_file(service_vendor) - if file_name: - day0_config_file = tenant_day0_dir + file_name - else: - if os.path.isdir(vendor_day0_dir): - file_name = get_config_file(service_vendor) - day0_config_file = vendor_day0_dir + file_name - else: - day0_config_file = '/fake_file_path' - - with open(day0_config_file) as _file: - try: - svm_context = yaml.load(_file) - except Exception as e: - msg = ("Failed yaml load file %s. Reason: %s" - % (day0_config_file, e)) - raise Exception(msg) - - msg = ("Loaded day0 config file %s for service_vendor %s," - "tenant_name %s" % (day0_config_file, service_vendor, - tenant_name)) - LOG.info(msg) - return svm_context - except Exception as ex: - msg = ("Failed to read day0 config file, ERROR: %s" % ex) - LOG.error(msg) - return None diff --git a/gbpservice/nfp/core/__init__.py b/gbpservice/nfp/core/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/nfp/core/cfg.py b/gbpservice/nfp/core/cfg.py deleted file mode 100644 index 22d96f1c8..000000000 --- a/gbpservice/nfp/core/cfg.py +++ /dev/null @@ -1,54 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - - -from oslo_config import cfg as oslo_config - -CONF = oslo_config.CONF - -NFP_OPTS = [ - oslo_config.IntOpt( - 'workers', - default=1, - help='Number of event worker process to be created.' - ), - oslo_config.ListOpt( - 'nfp_modules_path', - default='gbpservice.nfp.core.test', - help='Path for NFP modules.' - 'All modules from this path are autoloaded by framework' - ), - oslo_config.StrOpt( - 'backend', - default='rpc', - help='Backend Support for communicationg with configurator.' - ) -] - -EXTRA_OPTS = [ - oslo_config.StrOpt( - 'logger_class', - default='gbpservice.nfp.core.log.WrappedLogger', - help='logger class path to handle logging seperately.' - ), -] - - -def init(module, args, **kwargs): - """Initialize the configuration. """ - oslo_config.CONF.register_opts(EXTRA_OPTS) - oslo_config.CONF.register_opts(NFP_OPTS, module) - oslo_config.CONF(args=args, project='nfp', - version='%%(prog)s %s' % ('version'), - **kwargs) - - return oslo_config.CONF diff --git a/gbpservice/nfp/core/common.py b/gbpservice/nfp/core/common.py deleted file mode 100644 index 7edb5ea10..000000000 --- a/gbpservice/nfp/core/common.py +++ /dev/null @@ -1,68 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from oslo_config import cfg as oslo_cfg -from oslo_log import log as oslo_logging - -oslo_logging.register_options(oslo_cfg.CONF) - - -class Object(object): - pass - - -def init(): - """Initialize logging. """ - product_name = "nfp" - oslo_logging.setup(oslo_cfg.CONF, product_name) - - -def _is_class(obj): - return 'class' in str(type(obj)) - - -def _name(obj): - """Helper method to construct name of an object. - - 'module.class' if object is of type 'class' - 'module.class.method' if object is of type 'method' - """ - # If it is callable, then it is a method - if callable(obj): - return "{0}.{1}.{2}".format( - type(obj.__self__).__module__, - type(obj.__self__).__name__, - obj.__name__) - # If obj is of type class - elif _is_class(obj): - return "{0}.{1}".format( - type(obj).__module__, - type(obj).__name__) - else: - return obj.__name__ - - -def identify(obj): - """Helper method to display identity an object. - - Useful for logging. Decodes based on the type of obj. - Supports 'class' & 'method' types for now. - - :param obj: Object (Class/Method supported.) - Returns: String. Identification of the object. - """ - prefix = obj._NAME_ if hasattr(obj, '_NAME_') else '' - try: - return "([%s] %s)" % (prefix, _name(obj)) - except Exception: - # Some unknown type, returning empty - return "" diff --git a/gbpservice/nfp/core/context.py b/gbpservice/nfp/core/context.py deleted file mode 100644 index 21dd90cdb..000000000 --- a/gbpservice/nfp/core/context.py +++ /dev/null @@ -1,96 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import threading - - -class LogContext(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - return { - 'meta_id': self.data.get('meta_id', '-'), - 'nfi_id': self.data.get('nfi_id', '-'), - 'nfd_id': self.data.get('nfd_id', '-'), - 'path': self.data.get('path'), - 'auth_token': self.data.get('auth_token'), - 'namespace': self.data.get('namespace') - } - return self.data - - -class CoreContext(object): - - def __init__(self, data): - self.data = data - - def purge(self): - return { - 'log_context': LogContext(self.data.get('log_context')).purge(), - 'event_desc': self.data.get('event_desc') - } - - -class NfpContext(object): - - def __init__(self, data): - self.data = data - - def purge(self): - return CoreContext(self.data).purge() - - -Context = threading.local() - - -def init_log_context(): - return { - 'meta_id': '-', - 'nfi_id': '-', - 'nfd_id': '-', - 'path': '-', - 'auth_token': None, - 'namespace': None - } - - -def init(data=None): - if not data: - data = {} - if 'log_context' not in list(data.keys()): - data['log_context'] = init_log_context() - if 'event_desc' not in list(data.keys()): - data['event_desc'] = {} - Context.context = NfpContext(data) - context = getattr(Context, 'context') - return context.data - - -def get(): - try: - context = getattr(Context, 'context') - return context.data - except AttributeError: - return init() - - -def purge(): - try: - context = getattr(Context, 'context') - return context.purge() - except AttributeError: - init() - context = getattr(Context, 'context') - return context.purge() diff --git a/gbpservice/nfp/core/controller.py b/gbpservice/nfp/core/controller.py deleted file mode 100644 index e94d8d8d9..000000000 --- a/gbpservice/nfp/core/controller.py +++ /dev/null @@ -1,726 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import ast -import collections -import multiprocessing -import operator -import os -import pickle -import sys -import time -import zlib - -import eventlet -eventlet.monkey_patch() - -from oslo_config import cfg as oslo_config -from oslo_service import service as oslo_service -import six - -from gbpservice.nfp.core import cfg as nfp_cfg -from gbpservice.nfp.core import common as nfp_common -from gbpservice.nfp.core import context -from gbpservice.nfp.core import event as nfp_event -from gbpservice.nfp.core import launcher as nfp_launcher -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.core import manager as nfp_manager -from gbpservice.nfp.core import rpc as nfp_rpc -from gbpservice.nfp.core import worker as nfp_worker - -# REVISIT (mak): Unused, but needed for orchestrator, -# remove from here and add in orchestrator -from neutron.common import config - -LOG = nfp_logging.getLogger(__name__) -PIPE = multiprocessing.Pipe -PROCESS = multiprocessing.Process -identify = nfp_common.identify -deque = collections.deque - -# REVISIT (mak): fix to pass compliance check -config = config - -"""Implements NFP service. - - Base class for nfp modules, modules can invoke methods - of this class to interact with core. -""" - - -class NfpService(object): - - def __init__(self, conf): - self._conf = conf - self._event_handlers = nfp_event.NfpEventHandlers() - self._rpc_agents = {} - - def _make_new_event(self, event): - """Make a new event from the object passed. """ - desc = event.desc - event_dict = event.__dict__ - event = self.create_event(**event_dict) - event.desc.from_desc(desc) - return event - - def get_event_handlers(self): - return self._event_handlers - - def register_events(self, event_descs, priority=0): - """Register event handlers with core. """ - nfp_context = context.get() - module = nfp_context['log_context']['namespace'] - # REVISIT (mak): change name to register_event_handlers() ? - for event_desc in event_descs: - self._event_handlers.register( - event_desc.id, event_desc.handler, - module, priority=priority) - - def register_rpc_agents(self, agents): - """Register rpc handlers with core. """ - - for agent in agents: - topic = agent.topic - try: - self._rpc_agents[topic]['agents'].append(agent) - except KeyError: - self._rpc_agents[topic] = {} - self._rpc_agents[topic]['agents'] = [agent] - - def new_event(self, **kwargs): - """Define and return a new event. """ - return self.create_event(**kwargs) - - def create_event(self, **kwargs): - """To create a new event. """ - event = None - try: - event = nfp_event.Event(**kwargs) - except AssertionError as aerr: - message = "%s" % (aerr) - LOG.exception(message) - return event - - def post_graph(self, graph_nodes, root_node): - for node in graph_nodes: - self.post_event(node) - - self.post_event(root_node) - - def post_event(self, event, target=None): - """Post an event. - - As a base class, it only does the descriptor preparation. - NfpController class implements the required functionality. - """ - handler, module = ( - self._event_handlers.get_event_handler(event.id, module=target)) - assert handler, "No handler registered for event %s" % (event.id) - event.desc.type = nfp_event.SCHEDULE_EVENT - event.desc.flag = nfp_event.EVENT_NEW - event.desc.pid = os.getpid() - event.desc.target = module - if event.lifetime == -1: - event.lifetime = nfp_event.EVENT_DEFAULT_LIFETIME - if not event.context: - # Log nfp_context for event handling code - event.context = context.purge() - event.desc.path_type = event.context['event_desc'].get('path_type') - event.desc.path_key = event.context['event_desc'].get('path_key') - return event - - # REVISIT (mak): spacing=0, caller must explicitly specify - def poll_event(self, event, spacing=2, max_times=sys.maxsize): - """To poll for an event. - - As a base class, it only does the polling - descriptor preparation. - NfpController class implements the required functionality. - """ - nfp_context = context.get() - module = nfp_context['log_context']['namespace'] - handler, ev_spacing = ( - self._event_handlers.get_poll_handler(event.id, module=module)) - assert handler, "No poll handler found for event %s" % (event.id) - assert spacing or ev_spacing, "No spacing specified for polling" - if ev_spacing: - spacing = ev_spacing - if event.desc.type != nfp_event.POLL_EVENT: - event = self._make_new_event(event) - event.desc.uuid = event.desc.uuid + ":" + "POLL_EVENT" - event.desc.type = nfp_event.POLL_EVENT - event.desc.target = module - event.desc.flag = None - - kwargs = {'spacing': spacing, - 'max_times': max_times} - poll_desc = nfp_event.PollDesc(**kwargs) - - setattr(event.desc, 'poll_desc', poll_desc) - - if not event.context: - # Log nfp_context for event handling code - event.context = context.purge() - event.desc.path_type = event.context['event_desc'].get('path_type') - event.desc.path_key = event.context['event_desc'].get('path_key') - return event - - def event_complete(self, event, result=None): - """To declare and event complete. """ - try: - pickle.dumps(result) - uuid = event.desc.uuid - event = self._make_new_event(event) - event.desc.uuid = uuid - event.sequence = False - event.desc.flag = nfp_event.EVENT_COMPLETE - event.result = result - event.context = {} - event.data = {} - return event - except Exception as e: - raise e - - def create_work(self, work): - """Create a work, collection of events. """ - pass - - -"""NFP Controller class mixin other nfp classes. - - Nfp modules get the instance of this class when - they are initialized. - Nfp modules interact with core using the methods - of 'Service' class, whose methods are implemented - in this class. - Also, it mixes the other nfp core classes to complete - a nfp module request. -""" - - -class NfpController(nfp_launcher.NfpLauncher, NfpService): - - def __new__(cls, *args, **kwargs): - singleton = kwargs.get('singleton', True) - if singleton is False: - return object.__new__(cls, *args, **kwargs) - - if not hasattr(cls, '_instance'): - cls._instance = object.__new__(cls, *args, **kwargs) - else: - cls.__init__ = cls.__inited__ - return cls._instance - - def __inited__(self, conf): - pass - - def __init__(self, conf, singleton=True): - # Init the super classes. - nfp_launcher.NfpLauncher.__init__(self, conf) - NfpService.__init__(self, conf) - - # For book keeping - self._worker_process = {} - self._conf = conf - self._pipe = None - # Queue to stash events. - self._stashq = deque() - - self._manager = nfp_manager.NfpResourceManager(conf, self) - self._worker = nfp_worker.NfpWorker(conf) - - # ID of process handling this controller obj - self.PROCESS_TYPE = "distributor" - - def compress(self, event): - # REVISIT (mak) : zip only if length is > than threshold (1k maybe) - if not event.zipped: - event.zipped = True - data = {'context': event.context} - event.context = {} - if event.data: - data['data'] = event.data - event.data = zlib.compress(str(data)) - - def decompress(self, event): - if event.zipped: - try: - data = ast.literal_eval( - zlib.decompress(event.data)) - event.data = data.get('data') - event.context = data['context'] - event.zipped = False - except Exception as e: - message = "Failed to decompress event data, Reason: %r" % ( - e) - LOG.error(message) - raise e - - def is_picklable(self, event): - """To check event is picklable or not. - For sending event through pipe it must be picklable - """ - try: - pickle.dumps(event) - except Exception as e: - message = "(event - %s) is not picklable, Reason: %s" % ( - event.identify(), e) - assert False, message - - def pipe_recv(self, pipe): - event = None - try: - event = pipe.recv() - except Exception as exc: - LOG.debug("Failed to receive event from pipe " - "with exception - %r - will retry..", (exc)) - eventlet.greenthread.sleep(1.0) - if event: - self.decompress(event) - return event - - def pipe_send(self, pipe, event, resending=False): - self.is_picklable(event) - - try: - # If there is no reader yet - if not pipe.poll(): - self.compress(event) - pipe.send(event) - return True - except Exception as e: - message = ("Failed to send event - %s via pipe" - "- exception - %r - will resend" % ( - event.identify(), e)) - LOG.debug(message) - - # If the event is being sent by resending task - # then dont append here, task will put back the - # event at right location - if not resending: - # If couldnt send event.. stash it so that - # resender task will send event again - self._stashq.append(event) - return False - - def _fork(self, args): - proc = PROCESS(target=self.child, args=args) - proc.daemon = True - proc.start() - return proc - - def _resending_task(self): - while(True): - try: - event = self._stashq.popleft() - if self.PROCESS_TYPE != "worker": - evm = self._manager._get_event_manager(event.desc.worker) - LOG.debug("Resending event - %s", (event.identify())) - sent = self.pipe_send(evm._pipe, event, resending=True) - else: - sent = self.pipe_send(self._pipe, event, resending=True) - # Put back in front - if not sent: - self._stashq.appendleft(event) - except IndexError: - pass - except Exception as e: - message = ("Unexpected exception - %r - while" - "sending event - %s" % (e, event.identify())) - LOG.error(message) - - eventlet.greenthread.sleep(0.1) - - def _manager_task(self): - while True: - # Run 'Manager' here to monitor for workers and - # events. - self._manager.manager_run() - eventlet.greenthread.sleep(0.1) - - def _update_manager(self): - childs = self.get_childrens() - for pid, wrapper in six.iteritems(childs): - pipe = wrapper.child_pipe_map[pid] - # Inform 'Manager' class about the new_child. - self._manager.new_child(pid, pipe) - - def _process_event(self, event): - self._manager.process_events([event]) - - def get_childrens(self): - # oslo_process.ProcessLauncher has this dictionary, - # 'NfpLauncher' derives oslo_service.ProcessLauncher - return self.children - - def fork_child(self, wrap): - """Forks a child. - - Creates a full duplex pipe for child & parent - to communicate. - - Returns: Multiprocess object. - """ - - parent_pipe, child_pipe = PIPE(duplex=True) - - # Registered event handlers of nfp module. - # Workers need copy of this data to dispatch an - # event to module. - proc = self._fork(args=(wrap.service, parent_pipe, child_pipe, self)) - - message = ("Forked a new child: %d" - "Parent Pipe: % s, Child Pipe: % s") % ( - proc.pid, str(parent_pipe), str(child_pipe)) - LOG.info(message) - - try: - wrap.child_pipe_map[proc.pid] = parent_pipe - except AttributeError: - setattr(wrap, 'child_pipe_map', {}) - wrap.child_pipe_map[proc.pid] = parent_pipe - - self._worker_process[proc.pid] = proc - return proc.pid - - def launch(self, workers): - """Launch the controller. - - Uses Oslo Service to launch with configured #of workers. - Spawns a manager task to manager nfp events & workers. - - :param workers: #of workers to be launched - - Returns: None - """ - super(NfpController, self).launch_service( - self._worker, workers=workers) - - def post_launch(self): - """Post processing after workers launch. - - Tasks which needs to run only on distributor - process and any other resources which are not - expected to be forked are initialized here. - """ - self._update_manager() - - # create and launch rpc service agent for each topic - for key, value in six.iteritems(self._rpc_agents): - agents = value['agents'] - # Register NFP RPC managers in priority order, - # so that on rpc, oslo invokes them in the given order, - # This is required for NFP where multiple managers of - # different priority register for same rpc. - sorted_agents = sorted( - agents, key=operator.attrgetter('priority'), reverse=True) - rpc_managers = [agent.manager for agent in sorted_agents] - service = nfp_rpc.RpcService(topic=key, managers=rpc_managers) - # Launch rpc_service_agent - # Use threads for launching service - launcher = oslo_service.launch( - self._conf, service, workers=None) - - self._rpc_agents[key]['service'] = service - self._rpc_agents[key]['launcher'] = launcher - - # One task to manage the resources - workers & events. - eventlet.spawn_n(self._manager_task) - eventlet.spawn_n(self._resending_task) - # Oslo periodic task for state reporting - nfp_rpc.ReportStateTask(self._conf, self) - - def report_state(self): - """Invoked by report_task to report states of all agents. """ - for value in list(self._rpc_agents.values()): - for agent in value['agents']: - agent.report_state() - - def _verify_graph(self, graph): - """Checks for sanity of a graph definition. - - Checks if the same node is root node for - two subgraphs. - Unwinds graph and return two values - - graph signature and graph elements. - """ - graph_sig = {} - graph_nodes = [] - for parent, childs in six.iteritems(graph): - puuid = parent.desc.uuid - assert puuid not in list(graph_sig.keys()), ( - "Event - %s is already root of subgraph - %s" % ( - puuid, str(graph_sig[puuid]))) - graph_sig[puuid] = [] - for child in childs: - graph_sig[puuid].append(child.desc.uuid) - graph_nodes.append(child) - - return graph_sig, graph_nodes - - def post_graph(self, graph, root, graph_str=''): - """Post a new graph into the system. - - Graph is definition of events to be - dispatched in a particular pattern. - """ - graph_sig, graph_nodes = self._verify_graph(graph) - graph_data = { - 'id': root.desc.uuid + "_" + graph_str, - 'root': root.desc.uuid, - 'data': graph_sig} - - for graph_node in graph_nodes: - graph_node.desc.graph = graph_data - - root.desc.graph = graph_data - - super(NfpController, self).post_graph(graph_nodes, root) - - def post_event(self, event, target=None): - """Post a new event into the system. - - If distributor(main) process posts an event, it - is delivered to the worker. - If worker posts an event, it is deliverd to - distributor for processing, where it can decide - to loadbalance & sequence events. - - :param event: Object of 'Event' class. - - Returns: None - """ - event = super(NfpController, self).post_event(event, target=target) - message = "(event - %s) - New event" % (event.identify()) - LOG.debug(message) - if self.PROCESS_TYPE == "worker": - # Event posted in worker context, send it to parent process - message = ("(event - %s) - new event in worker" - "posting to distributor process") % (event.identify()) - - LOG.debug(message) - # Send it to the distributor process - self.pipe_send(self._pipe, event) - else: - message = ("(event - %s) - new event in distributor" - "processing event") % (event.identify()) - LOG.debug(message) - self._manager.process_events([event]) - - def poll_event(self, event, spacing=2, max_times=sys.maxsize): - """Post a poll event into the system. - - Core will poll for this event to timeout, after - timeout registered handler of module is invoked. - - :param event: Object of 'Event' class. - :param spacing: Spacing at which event should timeout. - :param max_times: Max #of times the event can timeout, - after the max_times, event is auto cancelled by - the core and the registered handler of module - is invoked. - - Returns: None - """ - # Poll event can only be posted by worker not by listener process - if self.PROCESS_TYPE != "worker": - message = "(event - %s) - poll event in distributor" % ( - event.identify()) - LOG.debug(message) - # 'Service' class to construct the poll event descriptor - event = super(NfpController, self).poll_event( - event, spacing=spacing, max_times=max_times) - self._manager.process_events([event]) - else: - ''' - # Only event which is delivered to a worker can be polled for, coz, - # after event timeouts, it should be delivered to the same worker, - # hence the check to make sure the correct event is been asked for - # polling. - assert event.desc.worker, "No worker for event %s" % ( - event.identify()) - LOG.debug("(event - %s) - poll event in worker" % - (event.identify())) - ''' - # 'Service' class to construct the poll event descriptor - event = super(NfpController, self).poll_event( - event, spacing=spacing, max_times=max_times) - # Send to the distributor process. - self.pipe_send(self._pipe, event) - - def stop_poll_event(self, key, id): - """To stop the running poll event - - :param key: key of polling event - :param id: id of polling event - """ - key = key + ":" + id + ":" + "POLL_EVENT" - event = self.new_event(id='STOP_POLL_EVENT', data={'key': key}) - event.desc.type = nfp_event.POLL_EVENT - event.desc.flag = nfp_event.POLL_EVENT_STOP - if self.PROCESS_TYPE == "worker": - self.pipe_send(self._pipe, event) - else: - self._manager.process_events([event]) - - def path_complete_event(self): - """Create event for path completion - """ - nfp_context = context.get() - event = self.new_event(id='PATH_COMPLETE') - event.desc.path_type = nfp_context['event_desc'].get('path_type') - event.desc.path_key = nfp_context['event_desc'].get('path_key') - if self.PROCESS_TYPE == "worker": - self.pipe_send(self._pipe, event) - else: - self._manager.process_events([event]) - - def event_complete(self, event, result=None): - """To mark an event complete. - - Module can invoke this API to mark an event complete. - a) Next event in sequence will be scheduled. - b) Event from cache is removed. - c) Polling for event is stopped. - d) If the worker dies before event is complete, the - event is scheduled to other available workers. - - :param event: Obj of 'Event' class - - Returns: None - """ - message = "(event - %s) complete" % (event.identify()) - LOG.debug(message) - event = super(NfpController, self).event_complete(event, result=result) - if self.PROCESS_TYPE == "distributor": - self._manager.process_events([event]) - else: - # Send to the distributor process. - self.pipe_send(self._pipe, event) - - -def load_nfp_modules(conf, controller): - modules_dirs = conf.nfp_modules_path - pymodules = [] - for _dir in modules_dirs: - pymodules.extend(load_nfp_modules_from_path(conf, controller, - _dir)) - return pymodules - - -def load_nfp_modules_from_path(conf, controller, path): - """ Load all nfp modules from configured directory. """ - pymodules = [] - nfp_context = context.get() - try: - base_module = __import__(path, - globals(), locals(), ['modules'], -1) - modules_dir = base_module.__path__[0] - try: - files = os.listdir(modules_dir) - for pyfile in set([f for f in files if f.endswith(".py")]): - try: - pymodule = __import__(path, - globals(), locals(), - [pyfile[:-3]], -1) - pymodule = eval('pymodule.%s' % (pyfile[:-3])) - try: - namespace = pyfile[:-3].split(".")[-1] - nfp_context['log_context']['namespace'] = namespace - pymodule.nfp_module_init(controller, conf) - pymodules += [pymodule] - message = "(module - %s) - Initialized" % ( - identify(pymodule)) - LOG.debug(message) - except AttributeError as e: - exc_type, exc_value, exc_traceback = sys.exc_info() - message = "Traceback: %s" % (exc_traceback) - LOG.error(message) - message = ("(module - %s) - does not implement" - "nfp_module_init()") % (identify(pymodule)) - LOG.warning(message) - except ImportError: - message = "Failed to import module %s" % (pyfile) - LOG.error(message) - except OSError: - message = "Failed to read files from %s" % (modules_dir) - LOG.error(message) - except ImportError: - message = "Failed to import module from path %s" % ( - path) - LOG.error(message) - - return pymodules - - -def controller_init(conf, nfp_controller): - nfp_controller.launch(conf.workers) - # Wait for conf.workers*1 + 1 secs for workers to comeup - time.sleep(conf.workers * 1 + 1) - nfp_controller.post_launch() - - -def nfp_modules_post_init(conf, nfp_modules, nfp_controller): - nfp_context = context.get() - for module in nfp_modules: - try: - namespace = module.__name__.split(".")[-1] - nfp_context['log_context']['namespace'] = namespace - module.nfp_module_post_init(nfp_controller, conf) - except AttributeError: - message = ("(module - %s) - does not implement" - "nfp_module_post_init(), ignoring") % (identify(module)) - LOG.debug(message) - - -def extract_module(args): - try: - index = args.index('--module') - module = args[index + 1] - args.remove('--module') - args.remove(module) - return args, module - except ValueError: - print("--module missing from cmd args") - sys.exit(-1) - - -def load_module_opts(conf): - module = conf.module - # register each opt from section - # to default section. - module_opts = eval('conf.%s.keys' % (module))() - for module_opt in module_opts: - module_cfg_opt = eval("conf.%s._group._opts['%s']['opt']" % ( - module, module_opt)) - module_cfg_opt_value = eval("conf.%s.%s" % (module, module_opt)) - conf.register_opt(module_cfg_opt) - conf.set_override(module_opt, module_cfg_opt_value) - - -def main(): - context.init() - args, module = extract_module(sys.argv[1:]) - conf = nfp_cfg.init(module, args) - conf.module = module - load_module_opts(conf) - nfp_logging.init_logger(oslo_config.CONF.logger_class) - nfp_common.init() - nfp_controller = NfpController(conf) - # Load all nfp modules from path configured - nfp_modules = load_nfp_modules(conf, nfp_controller) - # Init the controller, launch required contexts - controller_init(conf, nfp_controller) - # post_init of each module - nfp_modules_post_init(conf, nfp_modules, nfp_controller) - # Wait for every exec context to complete - nfp_controller.wait() diff --git a/gbpservice/nfp/core/event.py b/gbpservice/nfp/core/event.py deleted file mode 100644 index 4b23ca745..000000000 --- a/gbpservice/nfp/core/event.py +++ /dev/null @@ -1,412 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import collections -import multiprocessing -import uuid as pyuuid - -from gbpservice.nfp.core import common as nfp_common -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.core import module as nfp_api -from gbpservice.nfp.core import sequencer as nfp_seq - -LOG = nfp_logging.getLogger(__name__) -identify = nfp_common.identify - -"""Event Types """ -SCHEDULE_EVENT = 'schedule_event' -POLL_EVENT = 'poll_event' -STASH_EVENT = 'stash_event' - -"""Event Flag """ -EVENT_NEW = 'new_event' -EVENT_COMPLETE = 'event_done' -EVENT_ACK = 'event_ack' -POLL_EVENT_STOP = 'poll_event_stop' - -EVENT_DEFAULT_LIFETIME = 600 - -"""Sequencer status. """ -SequencerEmpty = nfp_seq.SequencerEmpty -SequencerBusy = nfp_seq.SequencerBusy - -deque = collections.deque - -"""Defines poll descriptor of an event. - - Holds all of the polling information of an - event. -""" - - -class PollDesc(object): - - def __init__(self, **kwargs): - # Spacing of the event, event will timeout @this spacing. - self.spacing = kwargs.get('spacing') - # Max times event can be polled, is autocancelled after. - self.max_times = kwargs.get('max_times') - # Reference to original event, UUID. - self.ref = kwargs.get('ref') - - -"""Defines the descriptor of an event. - - Holds the metadata for an event. Useful - for event processing. Not exposed to nfp modules. -""" - - -class EventDesc(object): - - def __init__(self, **kwargs): - # Unique id of the event, use what user passed or - # generate a new unique id. - uuid = kwargs.get('key', pyuuid.uuid4()) - id = kwargs.get('id', '') - - self.uuid = str(uuid) + ':' + id - - # see 'Event Types' - self.type = kwargs.get('type') - # see 'Event Flag' - self.flag = kwargs.get('flag') - # PID of worker which is handling this event - self.worker = kwargs.get('worker') - # Polling descriptor of event - self.poll_desc = kwargs.get('poll_desc') - # Target module to which this event must be delivered - self.target = None - # ID of graph of which this event is part of - self.graph = None - # Type of path to which this event belongs CREATE/UPDATE/DELETE - self.path_type = kwargs.get('path_type') - # Unique key for the path - self.path_key = kwargs.get('path_key') - # Marks whether an event was acked or not - self.acked = False - - def from_desc(self, desc): - self.type = desc.type - self.flag = desc.flag - self.worker = desc.worker - self.poll_desc = desc.poll_desc - self.path_type = desc.path_type - self.path_key = desc.path_key - - def to_dict(self): - return {'uuid': self.uuid, - 'type': self.type, - 'flag': self.flag, - 'worker': self.worker, - 'poll_desc': self.poll_desc, - 'path_type': self.path_type, - 'path_key': self.path_key - } - - -"""Defines the event structure. - - Nfp modules need to create object of the class - to create an event. -""" - - -class Event(object): - - def __init__(self, **kwargs): - # ID of event as passed by module - self.id = kwargs.get('id') - # Data blob - self.data = kwargs.get('data') - # Whether to sequence this event w.r.t - # other related events. - self.sequence = kwargs.get('serialize', False) - # Unique key to be associated with the event - self.key = kwargs.get('key') - # Binding key to define relation between - # different events. - self.binding_key = kwargs.get('binding_key') - # Handler of the event. - self.handler = kwargs.get('handler') - # Lifetime of the event in seconds. - self.lifetime = kwargs.get('lifetime', -1) - # Identifies whether event.data is zipped - self.zipped = False - # Log metadata context - self.context = kwargs.get('context', {}) - # Prepare the base descriptor - desc = kwargs.get('desc_dict') - if desc: - desc['key'] = self.key - desc['id'] = self.id - desc = EventDesc(**desc) - elif self.key: - desc = EventDesc(**{'key': self.key, - 'id': self.id}) - else: - desc = EventDesc(**{'id': self.id}) - self.desc = desc - - self.result = None - - cond = self.sequence is True and self.binding_key is None - assert not cond - - def identify(self): - if hasattr(self, 'desc'): - return "uuid=%s,id=%s,type=%s,flag=%s" % ( - self.desc.uuid, self.id, self.desc.type, self.desc.flag) - return "id=%s" % (self.id) - - -"""Table of event handler's. - - Maintains cache of every module's event handlers. - Also, maintains the polling against event_id - which are provided as decorators. -""" - - -class NfpEventHandlers(object): - - def __init__(self): - # {'event.id': [(event_handler, poll_handler, spacing)] - self._event_desc_table = {} - - def _log_meta(self, event_id, event_handler=None): - if event_handler: - return "(event_id - %s) - (event_handler - %s)" % ( - event_id, identify(event_handler)) - else: - return "(event_id - %s) - (event_handler - None)" % (event_id) - - def register(self, event_id, event_handler, module, priority=0): - """Registers a handler for event_id. - - Also fetches the decorated poll handlers if any - for the event and caches it. - """ - if not isinstance(event_handler, nfp_api.NfpEventHandler): - message = "%s - Handler is not instance of NfpEventHandler" % ( - self._log_meta(event_id, event_handler)) - LOG.error(message) - return - try: - poll_desc_table = event_handler.get_poll_desc_table() - poll_handler = poll_desc_table[event_id] - spacing = poll_handler._spacing - except KeyError: - # Default the poll handler and spacing values - poll_handler = event_handler.handle_poll_event - spacing = 0 - - try: - try: - self._event_desc_table[event_id]['modules'][module].append( - (event_handler, poll_handler, spacing, module)) - except KeyError: - self._event_desc_table[event_id]['modules'][module] = [ - (event_handler, poll_handler, spacing, module)] - try: - self._event_desc_table[event_id]['priority'][priority].append( - (event_handler, poll_handler, spacing, module)) - except KeyError: - self._event_desc_table[event_id]['priority'][priority] = [ - (event_handler, poll_handler, spacing, module)] - except KeyError: - self._event_desc_table[event_id] = {'modules': {}, 'priority': {}} - self._event_desc_table[event_id]['modules'][module] = [ - (event_handler, poll_handler, spacing, module)] - self._event_desc_table[event_id]['priority'][priority] = [ - (event_handler, poll_handler, spacing, module)] - message = "%s - Registered handler" % ( - self._log_meta(event_id, event_handler)) - LOG.debug(message) - - def get_event_handler(self, event_id, module=None): - """Get the handler for the event_id. """ - eh = None - rmodule = None - try: - if module: - eh = self._event_desc_table[event_id]['modules'][module][0][0] - rmodule = ( - self._event_desc_table[event_id]['modules'][module][0][3]) - else: - priorities = ( - list(self._event_desc_table[event_id]['priority'].keys())) - priority = max(priorities) - eh = ( - self._event_desc_table[ - event_id]['priority'][priority][0][0]) - rmodule = ( - self._event_desc_table[ - event_id]['priority'][priority][0][3]) - finally: - message = "%s - Returning event handler" % ( - self._log_meta(event_id, eh)) - LOG.debug(message) - return eh, rmodule - - def get_poll_handler(self, event_id, module=None): - """Get the poll handler for event_id. """ - ph, spacing = None, None - try: - if module: - ph = self._event_desc_table[event_id]['modules'][module][0][1] - spacing = self._event_desc_table[ - event_id]['modules'][module][0][2] - else: - priorities = ( - list(self._event_desc_table[event_id]['priority'].keys())) - priority = max(priorities) - ph = ( - self._event_desc_table[ - event_id]['priority'][priority][0][1]) - spacing = self._event_desc_table[ - event_id]['priority'][priority][0][2] - finally: - message = "%s - Returning poll handler" % ( - self._log_meta(event_id, ph)) - LOG.debug(message) - return ph, spacing - - def get_poll_spacing(self, event_id): - """Return the spacing for event_id. """ - spacing = 0 - try: - spacing = self._event_desc_table[event_id][0][2] - finally: - message = "%s - Poll spacing %d" % ( - self._log_meta(event_id), spacing) - LOG.debug(message) - return spacing - - -"""Manages the lifecycle of event of a process. - - Each process (worker/distributor) is associated - with a event manager. Event manager pulls events - from the pipe, caches it, sequences & dispatches - the events. -""" - - -class NfpEventManager(object): - - def __init__(self, conf, controller, sequencer, pipe=None, pid=-1): - self._conf = conf - self._controller = controller - # PID of process to which this event manager is associated - self._pid = pid - # Duplex pipe to read & write events - self._pipe = pipe - # Cache of UUIDs of events which are dispatched to - # the worker which is handled by this em. - self._cache = deque() - # Load on this event manager - num of events pending to be completed - self._load = 0 - - def _log_meta(self, event=None): - if event: - return "(event - %s) - (event_manager - %d)" % ( - event.identify(), self._pid) - else: - return "(event_manager - %d" % (self._pid) - - def _wait_for_events(self, pipe, timeout=0.01): - """Wait & pull event from the pipe. - - Wait till timeout for the first event and then - pull as many as available. - Returns: Events[] pulled from pipe. - """ - events = [] - try: - ret = pipe.poll(timeout) - if ret: - event = self._controller.pipe_recv(pipe) - if event: - events.append(event) - except multiprocessing.TimeoutError as err: - message = "%s" % (err) - LOG.exception(message) - return events - - def init_from_event_manager(self, em): - """Initialize from existing event manager. - - Invoked when an event manager has to take over - existing event manager. - - Whole cache is replaced and events are replayed. - This is used in case where a worker dies, dead - workers event manager is assigned to new worker. - """ - # Replay all the events from cache. - self._cache = em._cache - - def get_pending_events(self): - return list(self._cache) - - def get_load(self): - """Return current load on the manager.""" - return self._load - - def pop_event(self, event): - """Pop the passed event from cache. - - Is called when an event is complete/cancelled. - If the event was sequenced, then sequencer is - released to schedule next event. - - Removes event from cache. - """ - message = "%s - pop event" % (self._log_meta(event)) - LOG.debug(message) - try: - self._cache.remove(event.desc.uuid) - self._load -= 1 - except ValueError as verr: - verr = verr - message = "%s - event not in cache" % ( - self._log_meta(event)) - LOG.debug(message) - - def dispatch_event(self, event, event_type=None, - inc_load=True, cache=True): - """Dispatch event to the worker. - - Sends the event to worker through pipe. - Increments load if event_type is SCHEDULED event, - poll_event does not contribute to load. - """ - message = "%s - Dispatching to worker %d" % ( - self._log_meta(event), self._pid) - LOG.debug(message) - # Update the worker information in the event. - event.desc.worker = self._pid - # Update the event with passed type - if event_type: - event.desc.type = event_type - # Send to the worker - self._controller.pipe_send(self._pipe, event) - - self._load = (self._load + 1) if inc_load else self._load - # Add to the cache - if cache: - self._cache.append(event.desc.uuid) - - def event_watcher(self, timeout=0.01): - """Watch for events. """ - return self._wait_for_events(self._pipe, timeout=timeout) diff --git a/gbpservice/nfp/core/executor.py b/gbpservice/nfp/core/executor.py deleted file mode 100644 index cdd2db208..000000000 --- a/gbpservice/nfp/core/executor.py +++ /dev/null @@ -1,231 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from argparse import Namespace - -import six - -from gbpservice._i18n import _ -from gbpservice.nfp.core import context -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.core import threadpool as core_tp - -LOG = nfp_logging.getLogger(__name__) - - -class InUse(Exception): - - """Exception raised when same task executor instance - is fired twice or jobs - added after executor is fired. - """ - pass - - -def check_in_use(f): - """Check if instance of task executor is already - fired and executing jobs. - """ - - def wrapped(self, *args, **kwargs): - if self.fired: - raise InUse(_("Executor in use")) - return f(self, *args, **kwargs) - return wrapped - - -class TaskExecutor(object): - - """Executes given jobs in green threads. - - Any number of jobs can be added till executor - is fired. When fired, executes all jobs in - parallel in green threads. Waits for threads - to complete, captures the return values of thread - function. - Caller can choose to pass result_store where the - return value will be updated. - """ - - def __init__(self, jobs=0): - if not jobs: - self.thread_pool = core_tp.ThreadPool() - else: - self.thread_pool = core_tp.ThreadPool(thread_pool_size=jobs) - - self.pipe_line = [] - self.fired = False - - @check_in_use - def add_job(self, id, func, *args, **kwargs): - result_store = kwargs.pop('result_store', None) - - job = { - 'id': id, 'method': func, - 'args': args, 'kwargs': kwargs - } - - if result_store is not None: - job.update({'result_store': result_store}) - - LOG.debug("TaskExecutor - (job - %s) added to pipeline", - (str(job))) - - self.pipe_line.append(job) - - def _complete(self): - LOG.debug("TaskExecutor - complete") - self.pipe_line = [] - self.fired = False - - def dispatch(self, job): - context.init() - return job['method'](*job['args'], **job['kwargs']) - - @check_in_use - def fire(self): - self.fired = True - for job in self.pipe_line: - LOG.debug( - "TaskExecutor - (job - %s) dispatched", - (str(job))) - - th = self.thread_pool.dispatch(self.dispatch, job) - job['thread'] = th - - for job in self.pipe_line: - result = job['thread'].wait() - LOG.debug( - "TaskExecutor - (job - %s) complete", - (str(job))) - - job.pop('thread') - job['result'] = result - if 'result_store' in list(job.keys()): - job['result_store']['result'] = result - - done_jobs = self.pipe_line[:] - self._complete() - return done_jobs - - -class EventGraphExecutor(object): - - """Executor which executs a graph of events. - - An event graph can consist of events defined - in any combination of parallel and sequence - events. Executor will execute them in the - order and manner specified. - Eg., E1 -> (E2, E3) - [E1 should execute after E2, E3 completes, - while E2 & E3 can happen in parallel] - E2 -> (E4, E5) - [E2 should execute after E4, E5 completes, - while E4 & E5 should happen in sequence] - E3 -> (None) - [No child events for E3] - - Executor will run the above graph and execute events - in the exact specific order mentioned. - At each level, parent event holds the result of child - events, caller can use parent event complete notification - to get the child events execution status. - """ - - def __init__(self, manager): - self.manager = manager - self.running = {} - - def add(self, graph): - assert graph['id'] not in list(self.running.keys()), "Graph - %s \ - is already running" % (graph['id']) - graph['results'] = dict.fromkeys(graph['data']) - self.running[graph['id']] = graph - self.run(graph['id'], graph['root']) - - def run(self, graph_id, node): - graph = self.running[graph_id] - leafs = self._leafs(graph['data'], node) - if leafs == []: - results = self._results(graph, node) - self._schedule(node, results=results) - else: - self._dispatch(graph, leafs) - - def _results(self, graph, node): - try: - return self.running['results'][node] - except KeyError: - return [] - - def _dispatch(self, graph, nodes): - for node in nodes: - event = self.manager.get_event(node) - if event.sequence: - self._schedule(node) - else: - self.run(graph['id'], node) - - def _leafs(self, tree, root): - leafs = [] - try: - leafs = tree[root] - finally: - return leafs - - def _root(self, graph, of): - tree = graph['data'] - for root, nodes in six.iteritems(tree): - if of in nodes: - return root - return None - - def _schedule(self, node, results=None): - results = results or [] - event = self.manager.get_event(node) - event.result = results - self.manager._scheduled_new_event(event) - - def _graph(self, node): - for graph in list(self.running.values()): - root = self._root(graph, node) - if root: - return graph - - def _prepare_result(self, node, result): - result_obj = Namespace() - key, id = node.split(':') - result_obj.id = id - result_obj.key = key - result_obj.result = result - return result_obj - - def _update_result(self, graph, root, result): - if not graph['results'][root]: - graph['results'][root] = [] - graph['results'][root].append(result) - return graph['results'][root] - - def conntinue(self, completed_node, result): - graph = self._graph(completed_node) - if graph: - if completed_node == graph['root']: - # Graph is complete here, remove from running_instances - self.running.pop(graph['id']) - else: - root = self._root(graph, completed_node) - graph['data'][root].remove(completed_node) - result = self._prepare_result(completed_node, result) - results = self._update_result(graph, root, result) - if graph['data'][root] == []: - self._schedule(root, results=results) diff --git a/gbpservice/nfp/core/launcher.py b/gbpservice/nfp/core/launcher.py deleted file mode 100644 index eeda92137..000000000 --- a/gbpservice/nfp/core/launcher.py +++ /dev/null @@ -1,106 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import os -import signal -import time - -from oslo_service import service as oslo_service - -from gbpservice.nfp.core import log as nfp_logging - -LOG = nfp_logging.getLogger(__name__) -ProcessLauncher = oslo_service.ProcessLauncher - -"""Worker process launcher. - - Derives the oslo process launcher to - launch childrens with python multiprocessing - as oppose to os.fork(), coz, communication - is needed from parent->child not just the - parallel execution. -""" - - -class NfpLauncher(ProcessLauncher): - - def __init__(self, conf): - # Add SIGALARM to ignore_signals, because core - # uses SIGALRM for watchdog, while oslo uses the - # same for exit. - # Signal handler is singleton class, changing here will - # have global effect. - self.signal_handler = oslo_service.SignalHandler() - self.signal_handler._ignore_signals += ('SIGALRM',) - self.signal_handler._signals_by_name = dict( - (name, getattr(signal, name)) - for name in dir(signal) - if name.startswith("SIG") and - name not in self.signal_handler._ignore_signals) - - super(NfpLauncher, self).__init__(conf) - - def child(self, service, ppipe, cpipe, controller): - service.parent_pipe = ppipe - service.pipe = cpipe - service.controller = controller - self.launcher = self._child_process(service) - while True: - self._child_process_handle_signal() - status, signo = self._child_wait_for_exit_or_signal( - self.launcher) - if not oslo_service._is_sighup_and_daemon(signo): - self.launcher.wait() - break - self.launcher.restart() - - os._exit(status) - - def _start_child(self, wrap): - if len(wrap.forktimes) > wrap.workers: - # Limit ourselves to one process a second (over the period of - # number of workers * 1 second). This will allow workers to - # start up quickly but ensure we don't fork off children that - # die instantly too quickly. - if time.time() - wrap.forktimes[0] < wrap.workers: - time.sleep(1) - - wrap.forktimes.pop(0) - - wrap.forktimes.append(time.time()) - - pid = self.fork_child(wrap) - - message = "Started Child Process %d" % (pid) - LOG.debug(message) - - wrap.children.add(pid) - self.children[pid] = wrap - - return pid - - def fork_child(self, wrap): - # Default use os.fork to create a child - pid = os.fork() - if pid == 0: - self.launcher = self._child_process(wrap.service) - while True: - self._child_process_handle_signal() - status, signo = self._child_wait_for_exit_or_signal( - self.launcher) - if not oslo_service._is_sighup_and_daemon(signo): - self.launcher.wait() - break - self.launcher.restart() - - os._exit(status) - return pid diff --git a/gbpservice/nfp/core/log.py b/gbpservice/nfp/core/log.py deleted file mode 100644 index fb0a11c9d..000000000 --- a/gbpservice/nfp/core/log.py +++ /dev/null @@ -1,117 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import os -import sys - -from oslo_config import cfg as oslo_config -from oslo_log import log as oslo_logging -from oslo_utils import importutils - -from gbpservice.nfp.core import context - -EVENT = 50 -logging.addLevelName(EVENT, "EVENT") -CONF = oslo_config.CONF - - -if hasattr(sys, 'frozen'): # support for py2exe - _srcfile = "logging%s__init__%s" % (os.sep, __file__[-4:]) -elif __file__[-4:].lower() in ['.pyc', '.pyo']: - _srcfile = __file__[:-4] + '.py' -else: - _srcfile = __file__ -_srcfile = os.path.normcase(_srcfile) - - -def currentframe(): - """Return the frame object for the caller's stack frame.""" - try: - raise Exception - except Exception: - return sys.exc_info()[2].tb_frame.f_back - - -if hasattr(sys, '_getframe'): - currentframe = lambda: sys._getframe(3) - - -class NfpLogAdapter(oslo_logging.KeywordArgumentAdapter): - - def event(self, msg, *args, **kwargs): - self.log(EVENT, msg, *args, **kwargs) - - -class WrappedLogger(logging.Logger): - - def __init__(self, name): - logging.Logger.__init__(self, name) - - def findCaller(self): - """ - Find the stack frame of the caller so that we can note the source - file name, line number and function name. - """ - f = currentframe() - # On some versions of IronPython, currentframe() returns None if - # IronPython isn't run with -X:Frames. - if f is not None: - f = f.f_back - if f.f_back: - f = f.f_back - rv = "(unknown file)", 0, "(unknown function)" - while hasattr(f, "f_code"): - co = f.f_code - filename = os.path.normcase(co.co_filename) - if filename == _srcfile: - f = f.f_back - continue - rv = (co.co_filename, f.f_lineno, co.co_name) - break - return rv - - def _get_nfp_msg(self, msg): - nfp_context = context.get() - log_context = nfp_context['log_context'] - if log_context: - ctxt = "[%s] [NFI:%s] [NFD:%s]" % (log_context.get( - 'meta_id', '-'), - log_context.get('nfi_id', '-'), - log_context.get('nfd_id', '-')) - msg = "%s %s" % (ctxt, msg) - - component = '' - if hasattr(CONF, 'module'): - component = CONF.module - msg = "[%s] %s" % (component, msg) - return msg - - def makeRecord(self, name, level, fn, - lno, msg, args, exc_info, func=None, extra=None): - # Prefix log meta id with every log if project is 'nfp' - if extra and extra.get('project') == 'nfp': - msg = self._get_nfp_msg(msg) - return super(WrappedLogger, self).makeRecord( - name, level, fn, lno, msg, - args, exc_info, func=func, extra=extra) - - -def init_logger(logger_class): - logging.setLoggerClass(importutils.import_class(logger_class)) - - -def getLogger(name, **kwargs): - kwargs.update(project='nfp') - logger = NfpLogAdapter(logging.getLogger(name), - kwargs) - return logger diff --git a/gbpservice/nfp/core/manager.py b/gbpservice/nfp/core/manager.py deleted file mode 100644 index 49a54bde9..000000000 --- a/gbpservice/nfp/core/manager.py +++ /dev/null @@ -1,444 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import os - -import six - -from gbpservice.nfp.core import event as nfp_event -from gbpservice.nfp.core import executor as nfp_executor -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.core import path as nfp_path -from gbpservice.nfp.core import sequencer as nfp_sequencer -from gbpservice.nfp.core import watchdog as nfp_watchdog - -LOG = nfp_logging.getLogger(__name__) -NfpEventManager = nfp_event.NfpEventManager -NfpGraphExecutor = nfp_executor.EventGraphExecutor -WATCHDOG = nfp_watchdog.Watchdog - - -def IS_SCHEDULED_EVENT_ACK(event): - return event.desc.flag == nfp_event.EVENT_ACK - - -def IS_SCHEDULED_NEW_EVENT(event): - return event.desc.type == nfp_event.SCHEDULE_EVENT and ( - event.desc.flag == nfp_event.EVENT_NEW - ) - - -def IS_EVENT_COMPLETE(event): - return event.desc.flag == nfp_event.EVENT_COMPLETE - - -def IS_EVENT_GRAPH(event): - return event.desc.graph - - -def IS_PATH_COMPLETE_EVENT(event): - return event.id == 'PATH_COMPLETE' - - -"""Manages the forked childs. - - Invoked periodically, compares the alive childs with - snapshot and reports the difference to the caller. -""" - - -class NfpProcessManager(object): - - def __init__(self, conf, controller): - self._conf = conf - self._controller = controller - self._child_snapshot = [] - - def new_child(self, pid, pipe): - # Pass, as we will learn from comparision as watcher - pass - - def _dead(self, dead): - for proc in dead: - self._child_snapshot.remove(proc) - - def _new(self, new): - if new: - self._child_snapshot.extend(new) - - def child_watcher(self): - # Get the current set of childrens - current = self._controller.get_childrens() - set1 = set(current) - set2 = set(self._child_snapshot) - new = set1 - set2 - dead = set2 - set1 - - self._dead(dead) - self._new(new) - - return list(dead), list(new) - - -"""Manager for nfp resources. - - Manages all the nfp resources - process, events, polling queue etc. - Mixes the specific managers. -""" - - -class NfpResourceManager(NfpProcessManager, NfpEventManager): - - def __init__(self, conf, controller): - self._conf = conf - self._controller = controller - # Process, Event mixin, {'pid': event_manager} - self._resource_map = {} - # Cache of event objects - {'uuid':} - self._event_cache = {} - # watchdog object mapping with event id - {'uuid':} - self._watchdog_map = {} - # ID of the distributor process - self._distributor_process_id = os.getpid() - # Single sequencer to be used by all event managers - self._event_sequencer = nfp_sequencer.EventSequencer() - # Graph executor - self.graph_executor = NfpGraphExecutor(self) - - NfpProcessManager.__init__(self, conf, controller) - NfpEventManager.__init__(self, conf, controller, self._event_sequencer) - - def new_child(self, pid, pipe): - """Invoked when a new child is spawned. - - Associates an event manager with this child, maintains - the map. Manages the process. If this process is killed, - the event_manager is assigned to new process. - - :param process: Context of new process. - :param pipe: Pipe to communicate with this child. - """ - ev_manager = NfpEventManager( - self._conf, self._controller, - self._event_sequencer, - pipe=pipe, pid=pid) - self._resource_map.update(dict({pid: ev_manager})) - super(NfpResourceManager, self).new_child(pid, pipe) - - def manager_run(self): - """Invoked periodically to check on resources. - - a) Checks if childrens are active or any killed. - b) Checks if there are messages from any of workers. - c) Dispatches the events ready to be handled to workers. - """ - self._child_watcher() - self._event_watcher() - - def get_event(self, event_id): - return self._event_cache[event_id] - - def _event_acked(self, event): - """Post handling after event is dispatched to worker. """ - event.desc.acked = True - nfp_path.event_complete(event) - - def _dispatch_event(self, event): - """Dispatch event to a worker. """ - load_info = self._load_init() - event_manager, load_info = self._get_min_loaded_em(load_info) - event_manager.dispatch_event(event) - - def _graph_event(self, event): - if isinstance(event.desc.graph, dict): - graph = event.desc.graph - # root = graph['root'] - - event.desc.graph = graph['id'] - - self._event_cache[event.desc.uuid] = event - - if event.desc.uuid == graph['root']: - # graph = {'id': <>, 'data': {}, 'root': <>} - self.graph_executor.add(graph) - else: - graph = event.desc.graph - self.graph_executor.run(graph, event.desc.uuid) - - def _graph_event_complete(self, event): - self.graph_executor.conntinue(event.desc.uuid, event.result) - - def _scheduled_new_event(self, event): - # Cache the event object - self._event_cache[event.desc.uuid] = event - - # Event needs to be sequenced ? - if not event.sequence: - # Since event is dispatched, remove its - # graph link, modules may decide to use - # same event as non graph event - event.desc.graph = None - - decision = nfp_path.schedule_event(event) - if decision == 'schedule': - # Dispatch to a worker - self._dispatch_event(event) - LOG.debug("Watchdog started for event - %s", - (event.identify())) - self._watchdog(event) - elif decision == 'discard': - message = "Discarding path event - %s" % (event.identify()) - LOG.info(message) - self._controller.event_complete(event, result='FAILED') - else: - message = "(event - %s) - sequencing" % ( - event.identify()) - LOG.debug(message) - # Sequence the event which will be processed later - self._event_sequencer.sequence(event.binding_key, event) - - return event.sequence - - def _handle_path_complete(self, event): - try: - path_type = event.desc.path_type - path_key = event.desc.path_key - nfp_path.path_complete(path_type, path_key) - except Exception as e: - message = "Exception - %r - while handling"\ - "event - %s" % (e, event.identify()) - LOG.error(message) - - def event_expired(self, event=None): - if event: - LOG.debug("Watchdog expired for event - %s", (event.identify())) - self._watchdog_map.pop(event.desc.uuid, None) - self._controller.event_complete(event, result='FAILED') - - def _scheduled_event_ack(self, ack_event): - self._event_acked(ack_event) - - def _watchdog_cancel(self, event): - try: - LOG.debug("Watchdog cancelled for event - %s", (event.identify())) - wd = self._watchdog_map.pop(event.desc.uuid) - wd.cancel() - except KeyError: - pass - - def _watchdog(self, event, handler=None): - if not handler: - handler = self.event_expired - if event.lifetime != -1: - wd = WATCHDOG(handler, - seconds=event.lifetime, - event=event) - self._watchdog_map[event.desc.uuid] = wd - - def _scheduled_event_complete(self, event): - # Pop it from cache - cached_event = None - try: - cached_event = self._event_cache.pop(event.desc.uuid) - cached_event.result = event.result - # Mark the event as acked - self._watchdog_cancel(event) - # Get the em managing the event - evmanager = self._get_event_manager(event.desc.worker) - assert evmanager - evmanager.pop_event(event) - except KeyError as kerr: - kerr = kerr - message = "(event - %s) - completed, not in cache" % ( - event.identify()) - LOG.debug(message) - except AssertionError as aerr: - message = "%s" % (aerr.message) - LOG.debug(message) - finally: - # Release the sequencer for this sequence, - # so that next event can get scheduled. - self._event_sequencer.release(event.binding_key, event) - self._graph_event_complete(event) - - def _stop_poll_event(self, event): - try: - to_stop = event.data['key'] - event.desc.uuid = to_stop - self._watchdog_cancel(event) - except Exception as e: - message = "Exception - %r - while handling"\ - "event - %s" % (e, event.identify()) - LOG.error(message) - - def _non_schedule_event(self, event): - if event.desc.type == nfp_event.POLL_EVENT: - if event.desc.flag == nfp_event.POLL_EVENT_STOP: - self._stop_poll_event(event) - else: - message = "(event - %s) - polling for event, spacing(%d)" % ( - event.identify(), event.desc.poll_desc.spacing) - LOG.debug(message) - # If the poll event is generated without any parent - # event, then worker would not be pre-assigned. - # In such case, assign a random worker - if not event.desc.worker: - event.desc.worker = list(self._resource_map.keys())[0] - event.lifetime = event.desc.poll_desc.spacing - self._watchdog(event, handler=self._poll_timedout) - else: - message = "(event - %s) - Unknown non scheduled event" % ( - event.identify()) - LOG.error(message) - - def process_events_by_ids(self, event_ids): - for event_id in event_ids: - try: - event = self._event_cache[event_id] - self.process_events([event]) - except KeyError as kerr: - kerr = kerr - message = "%s - event missing in cache" % ( - event_id) - LOG.error(message) - - def process_events(self, events): - """Process the consumed event. - - Based on the event type, new event will - be added to cache, completed event is - removed from cache, poll event is added - to pollq. - - """ - for event in events: - message = "%s - processing event" % (event.identify()) - LOG.debug(message) - if IS_PATH_COMPLETE_EVENT(event): - self._handle_path_complete(event) - elif IS_SCHEDULED_EVENT_ACK(event): - self._scheduled_event_ack(event) - elif IS_SCHEDULED_NEW_EVENT(event): - if IS_EVENT_GRAPH(event): - self._graph_event(event) - else: - self._scheduled_new_event(event) - elif IS_EVENT_COMPLETE(event): - self._scheduled_event_complete(event) - else: - self._non_schedule_event(event) - - def _event_watcher(self): - """Watches for events for each event manager. - - Invokes each event manager to get events from workers. - Also checks parent process event manager. - """ - events = [] - # Get events from sequencer - events = self._event_sequencer.run() - events += nfp_path.run() - for pid, event_manager in six.iteritems(self._resource_map): - events += event_manager.event_watcher(timeout=0.01) - # Process the type of events received, dispatch only the - # required ones. - self.process_events(events) - - def _init_event_manager(self, from_em, to_em): - pending_event_ids = to_em.init_from_event_manager(from_em) - # Reprocess all the pending events, module handlers can - # continue processing of unacked events. - self.process_events_by_ids(pending_event_ids) - - def _replace_child(self, killed, new): - childrens = self._controller.get_childrens() - wrap = childrens[new] - pipe = wrap.child_pipe_map[new] - self.new_child(new, pipe) - new_em = self._resource_map[new] - killed_em = self._resource_map[killed] - new_em.init_from_event_manager(killed_em) - # Dispatch the pending events to the new worker through new em - self._replay_events(new_em) - - def _replay_events(self, event_manager): - pending_event_ids = event_manager.get_pending_events() - for event_id in pending_event_ids: - try: - message = "%s - replaying event" % (event_id) - LOG.info(message) - event_manager.dispatch_event( - self._event_cache[event_id], cache=False) - except KeyError as kerr: - kerr = kerr - message = "%s - eventid missing in cache" % ( - event_id) - LOG.error(message) - - def _child_watcher(self): - dead, new = super(NfpResourceManager, self).child_watcher() - if len(dead) and len(dead) != len(new): - message = "Killed process - %s, " - "New Process - %s, " - "does not match in count, few killed process" - "will not be replaced" % (str(dead), str(new)) - LOG.error(message) - - # Loop over dead workers and assign its - # event manager to one of the new worker - for killed_proc in dead: - new_proc = new.pop() - self._replace_child(killed_proc, new_proc) - del self._resource_map[killed_proc] - - def _load_init(self): - """Intializes load with current information. """ - load_info = [] - for pid, event_manager in six.iteritems(self._resource_map): - load = event_manager.get_load() - load_info.append([event_manager, load, pid]) - - return load_info - - def _get_min_loaded_em(self, load_info): - """Returns the min loaded event_manager. """ - minloaded = min(load_info, key=lambda x: x[1]) - load = minloaded[1] + 1 - load_info[load_info.index(minloaded)][1] = load - return minloaded[0], load_info - - def _get_event_manager(self, pid): - """Returns event manager of a process. """ - if pid == self._distributor_process_id: - return self - else: - return self._resource_map.get(pid) - - def _poll_timedout(self, event): - """Callback for poller when event timesout. """ - message = "(event - %s) - timedout" % (event.identify()) - LOG.debug(message) - - try: - evmanager = self._get_event_manager(event.desc.worker) - message = "(event-%s) event manager not found" % (event.identify()) - assert evmanager, message - if nfp_path.schedule_event(event) == 'schedule': - evmanager.dispatch_event(event, - event_type=nfp_event.POLL_EVENT, - inc_load=False, cache=False) - except AssertionError as aerr: - LOG.debug(aerr.message) - except Exception as e: - message = ("Unknown exception=%r - event=%s" % ( - e, event.identify())) - LOG.error(message) diff --git a/gbpservice/nfp/core/module.py b/gbpservice/nfp/core/module.py deleted file mode 100644 index e6bc0c007..000000000 --- a/gbpservice/nfp/core/module.py +++ /dev/null @@ -1,113 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from abc import abstractmethod - -import six - - -def poll_event_desc(*args, **kwargs): - """Poll Event Decorator. - - NFP modules can define the poll handlers using - this decorator. - """ - def decorator(f): - f._desc = True - f._spacing = kwargs.pop('spacing', 0) - f._event = kwargs.pop('event', None) - return f - - return decorator - - -"""Meta class. """ - - -class _Meta(type): - - def __init__(cls, names, bases, dict_): - """Metaclass that allows us to collect decorated periodic tasks.""" - super(_Meta, cls).__init__(names, bases, dict_) - - try: - cls._poll_desc_table = dict(cls._poll_desc_table) - except AttributeError: - cls._poll_desc_table = {} - - for value in list(cls.__dict__.values()): - if getattr(value, '_desc', False): - desc = value - cls._poll_desc_table[desc._event] = desc - - -"""Base class for nfp event handlers. - -Nfp modules derive and implement event handlers -of this class. -""" - - -@six.add_metaclass(_Meta) -class NfpEventHandler(object): - # __metaclass__ = ABCMeta - - def __init__(self): - super(NfpEventHandler, self).__init__() - - def get_poll_desc_table(self): - return self._poll_desc_table - - @abstractmethod - def handle_event(self, event): - """To handle an event. - - :param event: Object of 'Event' class. - - Returns: None - """ - pass - - @abstractmethod - def handle_poll_event(self, event): - """To handle a poll event. - - Core framework will inovke this method of event handler - when an event timesout. - - :param event: Object of 'Event' class. - - Returns: {'poll':True/False, 'event':} - 'poll': To repoll for the event. - 'event': Updated event, if not passed core will - repoll on the old event. - """ - pass - - @abstractmethod - def event_cancelled(self, event, reason): - """Notifies that an event is cancelled by core. - - Event could get cancelled, - a) Event expired. Module can set lifetime for - an event. If event is not complete with in - the time, it is auto expired by core. - b) Event max timedout. Module can set max number - of times to poll for an event. Event is cancelled - after the max times. - - :param event: Cancelled event. Object of 'Event' class. - :param reason: Reason for cancellation. String. - - Returns: None - """ - pass diff --git a/gbpservice/nfp/core/path.py b/gbpservice/nfp/core/path.py deleted file mode 100644 index a4f799449..000000000 --- a/gbpservice/nfp/core/path.py +++ /dev/null @@ -1,171 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import collections - -import six - -from gbpservice.nfp.core import log as nfp_logging - -deque = collections.deque - -LOG = nfp_logging.getLogger(__name__) - - -class Supress(object): - - def __init__(self, ignore_list=None): - self._ignore = ignore_list or [] - - def __enter__(self): - pass - - def __exit__(self, e_type, e_value, traceback): - if e_type in self._ignore: - return True - for exception in self._ignore: - if isinstance(e_type, exception): - return True - - -class Path(object): - - def __init__(self, name): - self._waitq = deque() - self.name = name - self.count = 0 - self.invalid = False - - def queue(self, event): - self._waitq.append(event) - - def pop(self): - events = [] - - with Supress([IndexError]): - events.append(self._waitq.popleft()) - return events - - def done(self): - self._waitq.clear() - - -# {'key': {'current':Path, 'waiting':Path} -paths = {} - - -def run(): - for key, path in six.iteritems(paths): - if path['current'].count == 0: - path['current'].done() - if path['waiting'].name != 'INVALID': - path['current'] = path['waiting'] - path['current'].invalid = False - path['waiting'] = Path('INVALID') - - events = [] - # Get any queued events in the current path - for key, path in six.iteritems(paths): - events += path['current'].pop() - return events - - -def event_complete(event): - name = event.desc.path_type - key = event.desc.path_key - - if not name: - return - name = name.upper() - with Supress([KeyError]): - path = paths[key] - if path['current'].name != name: - return - path['current'].count -= 1 - - -def schedule_event(event): - name = event.desc.path_type - key = event.desc.path_key - - if not name: - return 'schedule' - - name = name.upper() - - try: - path = paths[key] - if path['current'].name == name: - if path['current'].invalid: - return 'discard' - path['current'].count += 1 - return 'schedule' - - if path['waiting'].name == name: - path['waiting'].queue(event) - return 'wait' - - if path['current'].name != name: - return 'discard' - except Exception: - return 'schedule' - return 'schedule' - - -def path_complete(path_type, key): - try: - path = paths[key] - if path['current'].name == path_type.upper() and ( - path['waiting'].name == 'INVALID'): - paths.pop(key) - except KeyError: - message = "Path completion - %s path does not exist with key %s" % ( - path_type, key) - LOG.debug(message) - - -def create_path(key): - # Create cannot progress if there is already a path - # with the same key in any state - try: - path = paths[key] - assert False, "Path (%s) with key (%s) is already in progress" % ( - path['current'].name, key) - except KeyError: - # Create new path - paths[key] = {'current': Path('CREATE'), 'waiting': Path('INVALID')} - - -def delete_path(key): - try: - path = paths[key] - if path['current'].name != 'DELETE': - path['waiting'] = Path('DELETE') - path['current'].invalid = True - else: - assert False, ("Delete Path (%s) with key (%s)" - "is already in progress" % ( - path['current'].name, key)) - except KeyError: - paths[key] = {'current': Path('DELETE'), 'waiting': Path('INVALID')} - - -def update_path(key): - # Update cannot progress if there is DELETE already in progress - # or DELETE already waiting. - try: - path = paths[key] - assert False, "Path (%s) with key (%s) is in progress" % ( - path.name, key) - except KeyError: - # Create new path - paths[key] = {'current': Path('UPDATE'), 'waiting': Path('INVALID')} diff --git a/gbpservice/nfp/core/rpc.py b/gbpservice/nfp/core/rpc.py deleted file mode 100644 index 6c0c21684..000000000 --- a/gbpservice/nfp/core/rpc.py +++ /dev/null @@ -1,171 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - - -from neutron.agent import rpc as n_agent_rpc -from neutron_lib import context as n_context -from neutron_lib import rpc as n_rpc -from oslo_config import cfg as oslo_config -from oslo_service import loopingcall as oslo_looping_call -from oslo_service import periodic_task as oslo_periodic_task -from oslo_service import service as oslo_service - -from gbpservice.nfp.core import log as nfp_logging - -LOG = nfp_logging.getLogger(__name__) - -n_rpc.init(oslo_config.CONF) - -"""RPC Service class. - - This class is implemented from neutron.common.rpc - because neutron limit itself to single rpc manager - per topic while oslo allows multiple. - Using this class, one can register multiple rpc - managers with oslo. -""" - - -class RpcService(oslo_service.Service): - - def __init__(self, topic, managers=None): - # Not taking host and serializer as neutron rpc does not support - # passing any host and serializer- it picks up the host from - # 'cfg.CONF.host' and consider serializer as None - - super(RpcService, self).__init__() - self.topic = topic - self.endpoints = managers or [] - - def start(self): - LOG.debug("RPCAgent listening on topic=%s", self.topic) - super(RpcService, self).start() - - self.conn = n_rpc.create_connection() - LOG.debug("Creating Consumer connection for Service %s", - self.topic) - - self.conn.create_consumer(self.topic, self.endpoints) - - # Hook to allow the manager to do other initializations after - # the rpc connection is created. - for manager in self.endpoints: - if callable(getattr(manager, 'initialize_service_hook', None)): - self.manager.initialize_service_hook(self) - - # Consume from all consumers in threads - self.conn.consume_in_threads() - - def stop(self): - # Try to shut the connection down, but if we get any sort of - # errors, go ahead and ignore them.. as we're shutting down anyway - try: - self.conn.close() - except Exception: - pass - super(RpcService, self).stop() - - -"""Wrapper class for Neutron RpcAgent definition. - - NFP modules will use this class for the agent definition. - Associates the state reporting of agent to ease - the usage for modules. -""" - - -class RpcAgent(object): - - def __init__( - self, sc, host=None, - topic=None, manager=None, report_state=None, priority=0): - # report_state = - # {, 'plugin_topic': '', 'report_interval': ''} - - self.sc = sc - # The argument 'host' will be ignored as neutron rpc does not - # support passing any host -it picks up from 'cfg.CONF.host' - self.host = oslo_config.CONF.host - self.topic = topic - self.manager = manager - self.priority = priority - # Check if the agent needs to report state - if report_state: - self._report_state = ReportState(report_state) - - def report_state(self): - if hasattr(self, '_report_state'): - LOG.debug("Agent (%s) reporting state", - (self.identify())) - self._report_state.report() - - def identify(self): - return "(host=%s,topic=%s)" % (self.host, self.topic) - - -"""This class implements the state reporting for neutron *aaS agents - - One common place of handling of reporting logic. - Each nfp module just need to register the reporting data and - plugin topic. -""" - - -class ReportState(object): - - def __init__(self, data): - self._n_context = n_context.get_admin_context_without_session() - self._data = data - self._topic = data.pop('plugin_topic', None) - self._interval = data.pop('report_interval', 0) - self._state_rpc = n_agent_rpc.PluginReportStateAPI( - self._topic) - - def report(self): - try: - LOG.debug("Reporting state with data (%s)", - (self._data)) - self._state_rpc.report_state(self._n_context, self._data) - self._data.pop('start_flag', None) - except AttributeError: - # This means the server does not support report_state - message = "Neutron server does not support state report." - "Agent State reporting will be disabled" - LOG.info(message) - return - except Exception: - message = "Stopped reporting agent state!" - LOG.exception(message) - - -"""Periodic task to report neutron *aaS agent state. - - Derived from oslo periodic task, to report the agents state - if any, to neutron *aaS plugin. -""" - - -class ReportStateTask(oslo_periodic_task.PeriodicTasks): - - def __init__(self, conf, controller): - super(ReportStateTask, self).__init__(conf) - self._controller = controller - # Start a looping at the defined pulse - pulse = oslo_looping_call.FixedIntervalLoopingCall( - self.run_periodic_tasks, None, None) - pulse.start( - interval=5, initial_delay=None) - - @oslo_periodic_task.periodic_task(spacing=10) - def report_state(self, context): - # trigger the state reporting - self._controller.report_state() diff --git a/gbpservice/nfp/core/sequencer.py b/gbpservice/nfp/core/sequencer.py deleted file mode 100644 index 573fd7776..000000000 --- a/gbpservice/nfp/core/sequencer.py +++ /dev/null @@ -1,138 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import collections - -import six - -from gbpservice.nfp.core import log as nfp_logging - -LOG = nfp_logging.getLogger(__name__) - -deque = collections.deque - - -class SequencerEmpty(Exception): - pass - - -class SequencerBusy(Exception): - pass - - -"""Sequences the events. """ - - -class EventSequencer(object): - - class Sequencer(object): - - def __init__(self): - # Events not scheduled are queued - self._waitq = deque() - # Currently scheduled event - self._scheduled = None - - def _is_busy(self): - if self._scheduled: - raise SequencerBusy - - def _is_empty(self): - if not len(self._waitq): - raise SequencerEmpty - - def sequence(self, event): - self._waitq.append(event) - - def run(self): - """Run to get event to be scheduled. - - If sequencer is busy - i.e, an event is already - scheduled and in progress raises busy except. - If sequencer is empty - i.e, no event in sequencer - raises empty except. - """ - self._is_busy() - self._is_empty() - # Pop the first element in the queue - FIFO - self._scheduled = self._waitq.popleft() - return self._scheduled - - def is_scheduled(self, event): - if self._scheduled: - return self._scheduled.desc.uuid == event.desc.uuid and ( - self._scheduled.id == event.id) - return True - - def release(self): - self._scheduled = None - - def pop(self): - self.release() - events = list(self._waitq) - self._waitq.clear() - return events - - def __init__(self): - # Sequence of related events - # {key: sequencer()} - self._sequencer = {} - - def sequence(self, key, event): - try: - self._sequencer[key].sequence(event) - except KeyError: - self._sequencer[key] = self.Sequencer() - self._sequencer[key].sequence(event) - message = "Sequenced event - %s" % (event.identify()) - LOG.debug(message) - - def run(self): - events = [] - # Loop over copy and delete from original - sequencers = dict(self._sequencer) - for key, sequencer in six.iteritems(sequencers): - try: - event = sequencer.run() - if event: - message = "Desequenced event - %s" % ( - event.identify()) - LOG.debug(message) - event.sequence = False - events.append(event) - except SequencerBusy as exc: - pass - except SequencerEmpty as exc: - exc = exc - message = "Sequencer empty" - LOG.debug(message) - del self._sequencer[key] - return events - - def pop(self): - events = [] - sequencers = dict(self._sequencer) - for key, sequencer in six.iteritems(sequencers): - events += sequencer.pop() - return events - - def release(self, key, event): - try: - message = "(event - %s) checking to release" % (event.identify()) - LOG.debug(message) - if self._sequencer[key].is_scheduled(event): - message = "(event - %s) Releasing sequencer" % ( - event.identify()) - LOG.debug(message) - self._sequencer[key].release() - except KeyError: - return diff --git a/gbpservice/nfp/core/threadpool.py b/gbpservice/nfp/core/threadpool.py deleted file mode 100644 index 535552544..000000000 --- a/gbpservice/nfp/core/threadpool.py +++ /dev/null @@ -1,99 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import os - -import eventlet -from eventlet import greenpool -from eventlet import greenthread - -from gbpservice.nfp.core import log as nfp_logging - -LOG = nfp_logging.getLogger(__name__) - - -def _thread_done(gt, *args, **kwargs): - kwargs['pool'].thread_done(kwargs['thread']) - - -"""Descriptor class for green thread """ - - -class Thread(object): - - def __init__(self, thread, pool): - self.thread = thread - self.thread.link(_thread_done, pool=pool, thread=self) - - def stop(self): - self.thread.kill() - - def wait(self): - return self.thread.wait() - - def link(self, func, *args, **kwargs): - self.thread.link(func, *args, **kwargs) - - def identify(self): - return "(%d -> %s)" % (os.getpid(), 'Thread') - - -"""Abstract class to manage green threads """ - - -class ThreadPool(object): - - def __init__(self, thread_pool_size=10): - self.pool = greenpool.GreenPool(thread_pool_size) - self.threads = [] - - def dispatch(self, callback, *args, **kwargs): - """Invokes the specified function in one of the thread """ - gt = self.pool.spawn(callback, *args, **kwargs) - th = Thread(gt, self) - self.threads.append(th) - return th - - def thread_done(self, thread): - """Invoked when thread is complete, remove it from cache """ - self.threads.remove(thread) - - def stop(self): - """To stop the thread """ - current = greenthread.getcurrent() - - # Make a copy - for x in self.threads[:]: - if x is current: - # Skipping the current thread - continue - try: - x.stop() - except Exception as ex: - message = "Exception - %s" % (ex) - LOG.exception(message) - - def wait(self): - """Wait for the thread """ - current = greenthread.getcurrent() - - # Make a copy - for x in self.threads[:]: - if x is current: - continue - try: - x.wait() - except eventlet.greenlet.GreenletExit: - pass - except Exception as ex: - message = "Unexpected exception - %r" % (ex) - LOG.error(message) diff --git a/gbpservice/nfp/core/watchdog.py b/gbpservice/nfp/core/watchdog.py deleted file mode 100644 index e0427dc9e..000000000 --- a/gbpservice/nfp/core/watchdog.py +++ /dev/null @@ -1,113 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - - -import heapq -import signal -from time import time - -from gbpservice.nfp.core import log as nfp_logging - - -LOG = nfp_logging.getLogger(__name__) - -alarmlist = [] - -__new_alarm = lambda t, f, a, k: (t + time(), f, a, k) -__next_alarm = lambda: int( - round(alarmlist[0][0] - time())) if alarmlist else None -__set_alarm = lambda: signal.alarm(max(__next_alarm(), 1)) - - -class Watchdog(object): - - def __init__(self, callback, seconds=20 * 60, **kwargs): - self._seconds = seconds - self._callback = callback - self.kwargs = kwargs - - self._alarm = alarm(self._seconds, self.timedout) - - def timedout(self): - try: - self._callback(**self.kwargs) - except Exception as e: - message = "Unexpected exception - %s" % (e) - LOG.error(message) - - def cancel(self): - try: - cancel(self._alarm) - except ValueError: - pass - except Exception as e: - message = "Unexpected exception - %s" % (e) - LOG.error(message) - - -def __clear_alarm(): - """Clear an existing alarm. - - If the alarm signal was set to a callable other than our own, queue the - previous alarm settings. - """ - oldsec = signal.alarm(0) - oldfunc = signal.signal(signal.SIGALRM, __alarm_handler) - if oldsec > 0 and oldfunc != __alarm_handler: - heapq.heappush(alarmlist, (__new_alarm(oldsec, oldfunc, [], {}))) - - -def __alarm_handler(*zargs): - """Handle an alarm by calling any due heap entries and resetting the alarm. - - Note that multiple heap entries might get called, especially if calling an - entry takes a lot of time. - """ - try: - nextt = __next_alarm() - while nextt is not None and nextt <= 0: - (tm, func, args, keys) = heapq.heappop(alarmlist) - func(*args, **keys) - nextt = __next_alarm() - finally: - if alarmlist: - __set_alarm() - - -def alarm(sec, func, *args, **keys): - """Set an alarm. - - When the alarm is raised in `sec` seconds, the handler will call `func`, - passing `args` and `keys`. Return the heap entry (which is just a big - tuple), so that it can be cancelled by calling `cancel()`. - """ - __clear_alarm() - try: - newalarm = __new_alarm(sec, func, args, keys) - heapq.heappush(alarmlist, newalarm) - return newalarm - finally: - __set_alarm() - - -def cancel(alarm): - """Cancel an alarm by passing the heap entry returned by `alarm()`. - - It is an error to try to cancel an alarm which has already occurred. - """ - __clear_alarm() - try: - alarmlist.remove(alarm) - heapq.heapify(alarmlist) - finally: - if alarmlist: - __set_alarm() diff --git a/gbpservice/nfp/core/worker.py b/gbpservice/nfp/core/worker.py deleted file mode 100644 index 9a0694795..000000000 --- a/gbpservice/nfp/core/worker.py +++ /dev/null @@ -1,222 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import os -import sys -import time -import traceback - -import eventlet -import greenlet -from oslo_service import service as oslo_service - -from gbpservice.nfp.core import common as nfp_common -from gbpservice.nfp.core import context -from gbpservice.nfp.core import event as nfp_event -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.core import watchdog as nfp_watchdog - -LOG = nfp_logging.getLogger(__name__) -Service = oslo_service.Service -identify = nfp_common.identify -WATCHDOG = nfp_watchdog.Watchdog - -DEFAULT_THREAD_TIMEOUT = (10 * 60) - -"""Implements worker process. - - Derives from oslo service. - Implements the worker functionality. - Waits for the events from distributor, handles them, - invokes the registered event handler in a thread. -""" - - -class NfpWorker(Service): - - def __init__(self, conf, threads=10): - # REVISIT(mak): Should #threads be a conf ? - Service.__init__(self, threads=threads) - # Parent end of duplex pipe - self.parent_pipe = None - # Pipe to recv/send messages to distributor - self.pipe = None - # Cache of event handlers - self.controller = None - self._conf = conf - self._threads = threads - - def start(self): - """Service start, runs here till dies. - - When a oslo service is launched, this method - is invoked. - Polls for messages from distributor and process - them. - """ - # Update the process type in controller. - self.controller.PROCESS_TYPE = "worker" - self.controller._pipe = self.pipe - self.event_handlers = self.controller.get_event_handlers() - - eventlet.spawn_n(self.controller._resending_task) - - while True: - try: - event = None - if self.pipe.poll(0.1): - event = self.controller.pipe_recv(self.pipe) - if event: - message = "%s - received event" % ( - self._log_meta(event)) - LOG.debug(message) - self.controller.decompress(event) - self._process_event(event) - except Exception as e: - message = "Exception - %s" % (e) - LOG.error(message) - # Yeild cpu - time.sleep(0) - - def _log_meta(self, event=None): - if event: - return "(event - %s) - (worker - %d)" % ( - event.identify(), os.getpid()) - else: - return "(worker - %d)" % (os.getpid()) - - def _send_event_ack(self, event): - # Create new event from existing one - ack_event = nfp_event.Event(id=event.id) - ack_event.id = event.id - desc = nfp_event.EventDesc(**event.desc.__dict__) - desc.uuid = event.desc.uuid - desc.flag = nfp_event.EVENT_ACK - setattr(ack_event, 'desc', desc) - self.controller.pipe_send(self.pipe, ack_event) - - def _process_event(self, event): - """Process & dispatch the event. - - Decodes the event type and performs the required - action. - Executes the registered event handler in one of the - thread. - """ - if event.desc.type == nfp_event.SCHEDULE_EVENT: - eh, _ = ( - self.event_handlers.get_event_handler( - event.id, module=event.desc.target)) - self.dispatch(eh.handle_event, event, eh=eh) - elif event.desc.type == nfp_event.POLL_EVENT: - self.dispatch(self._handle_poll_event, event) - - def _repoll(self, ret, event, eh): - if ret.get('poll', False): - message = ("(event - %s) - repolling event -" - "pending times - %d") % ( - event.identify(), event.desc.poll_desc.max_times) - LOG.debug(message) - if event.desc.poll_desc.max_times: - self.controller.poll_event( - event, - spacing=event.desc.poll_desc.spacing, - max_times=event.desc.poll_desc.max_times) - else: - message = ("(event - %s) - max timed out," - "calling event_cancelled") % (event.identify()) - LOG.debug(message) - eh.event_cancelled(event, 'MAX_TIMED_OUT') - - def _handle_poll_event(self, event): - ret = {'poll': False} - event.desc.poll_desc.max_times -= 1 - module = event.desc.target - poll_handler, _ = ( - self.event_handlers.get_poll_handler(event.id, module=module)) - event_handler, _ = ( - self.event_handlers.get_event_handler(event.id, module=module)) - try: - try: - ret = poll_handler(event) - except TypeError: - ret = poll_handler(event_handler, event) - if not ret: - ret = {'poll': True} - except greenlet.GreenletExit: - pass - except Exception as exc: - message = "Exception - %r" % (exc) - LOG.error(message) - ret = self.dispatch_exception(event_handler, event, exc) - if not ret: - ret = {'poll': False} - - self._repoll(ret, event, event_handler) - - def _dispatch(self, handler, event, *args, **kwargs): - event.context['log_context']['namespace'] = event.desc.target - context.init(event.context) - try: - handler(event, *args) - except greenlet.GreenletExit: - self.controller.event_complete(event, result='FAILED') - except Exception as exc: - # How to log traceback propery ?? - message = "Exception - %r" % (exc) - LOG.error(message) - self.dispatch_exception(kwargs.get('eh'), event, exc) - self.controller.event_complete(event, result="FAILED") - finally: - self._send_event_ack(event) - - def dispatch_exception(self, event_handler, event, exception): - ret = {} - try: - ret = event_handler.handle_exception(event, exception) - except Exception: - exc_type, exc_value, exc_traceback = sys.exc_info() - message = "Traceback: %s" % traceback.format_exception( - exc_type, exc_value, exc_traceback) - LOG.error(message) - finally: - return ret - - def thread_done(self, th, watchdog=None): - if watchdog: - watchdog.cancel() - - def thread_timedout(self, thread=None): - if thread: - eventlet.greenthread.kill(thread.thread) - - def dispatch(self, handler, event, *args, **kwargs): - if self._threads: - th = self.tg.add_thread( - self._dispatch, handler, event, *args, **kwargs) - message = "%s - (handler - %s) - dispatched to thread " % ( - self._log_meta(), identify(handler)) - LOG.debug(message) - wd = WATCHDOG(self.thread_timedout, - seconds=DEFAULT_THREAD_TIMEOUT, thread=th) - th.link(self.thread_done, watchdog=wd) - else: - try: - handler(event, *args) - message = "%s - (handler - %s) - invoked" % ( - self._log_meta(), identify(handler)) - LOG.debug(message) - self._send_event_ack(event) - except Exception as exc: - message = "Exception from module's event handler - %s" % (exc) - LOG.error(message) - self.dispatch_exception(kwargs.get('eh'), event, exc) diff --git a/gbpservice/nfp/lib/__init__.py b/gbpservice/nfp/lib/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/nfp/lib/nfp_context_manager.py b/gbpservice/nfp/lib/nfp_context_manager.py deleted file mode 100644 index ef5cc1974..000000000 --- a/gbpservice/nfp/lib/nfp_context_manager.py +++ /dev/null @@ -1,214 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - - -import time - -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.lib import nfp_exceptions - -LOG = nfp_logging.getLogger(__name__) - -sql_lock_support = True - - -class ContextManager(object): - - def __init__(self, session=None, suppress=tuple()): - # suppress tuple holds the kind of exceptions - # the we don't have re-raise - self.session = session - self.suppress = suppress - - def __enter__(self): - pass - - def __exit__(self, Exptype, expvalue, traceback): - - if self.suppress and Exptype: - if Exptype in self.suppress: - return False - for exception in self.suppress: - if isinstance(Exptype, exception): - return False - if not self.suppress and traceback: - return True - else: - return False - - def retry(self, method, *args, **kwargs): - tries = kwargs.pop('tries', 1) - delay = 2 - backoff = 2 - while tries > 1: - # Loop for 'tries-1' times and - # the last time without any try-catch - try: - return method(*args, **kwargs) - except Exception: - msg = " %s retrying in %s seconds " % (self.__class__, delay) - LOG.error(msg) - - time.sleep(delay) - tries -= 1 - delay *= backoff - return method(*args, **kwargs) - - -class NfpDbContextManager(ContextManager): - - def new(self, **kwargs): - return NfpDbContextManager(**kwargs) - - def lock(self, session, method, *args, **kwargs): - if not sql_lock_support: - return method(session, *args, **kwargs) - with session.begin(subtransactions=True): - session.execute("SELECT GET_LOCK('nfp_db_lock', -1)") - ret = method(session, *args, **kwargs) - session.execute("SELECT RELEASE_LOCK('nfp_db_lock')") - return ret - - def __enter__(self): - super(NfpDbContextManager, self).__enter__() - return self - - def __exit__(self, Exptype, expvalue, traceback): - if super(NfpDbContextManager, self).__exit__( - Exptype, expvalue, traceback): - raise nfp_exceptions.DbException(Exptype, str(expvalue), traceback) - - # By default exit method returns False, if False is returned - # the with block re-raises the exception. To suppress that - # True should be returned explicitly - - return True - - -class NfpNovaContextManager(ContextManager): - - def new(self, **kwargs): - return NfpNovaContextManager(**kwargs) - - def __enter__(self): - super(NfpNovaContextManager, self).__enter__() - return self - - def __exit__(self, Exptype, expvalue, traceback): - if super(NfpNovaContextManager, self).__exit__( - Exptype, expvalue, traceback): - raise nfp_exceptions.NovaException( - Exptype, str(expvalue), traceback) - - # By default exit method returns False, if False is returned - # the with block re-raises the exception. To suppress that - # True should be returned explicitly - - return True - - -class NfpKeystoneContextManager(ContextManager): - - def new(self, **kwargs): - return NfpKeystoneContextManager(**kwargs) - - def __enter__(self): - super(NfpKeystoneContextManager, self).__enter__() - return self - - def __exit__(self, Exptype, expvalue, traceback): - if super(NfpKeystoneContextManager, self).__exit__( - Exptype, expvalue, traceback): - raise nfp_exceptions.KeystoneException( - Exptype, str(expvalue), traceback) - # By default exit method returns False, if False is returned - # the with block re-raises the exception. To suppress that - # True should be returned explicitly - - return True - - -class NfpNeutronContextManager(ContextManager): - - def new(self, **kwargs): - return NfpNeutronContextManager(**kwargs) - - def __enter__(self): - super(NfpNeutronContextManager, self).__enter__() - return self - - def __exit__(self, Exptype, expvalue, traceback): - if super(NfpNeutronContextManager, self).__exit__( - Exptype, expvalue, traceback): - raise nfp_exceptions.NeutronException( - Exptype, str(expvalue), traceback) - - # By default exit method returns False, if False is returned - # the with block re-raises the exception. To suppress that - # True should be returned explicitly - - return True - - -class NfpHeatContextManager(ContextManager): - - def new(self, **kwargs): - return NfpHeatContextManager(**kwargs) - - def __enter__(self): - super(NfpHeatContextManager, self).__enter__() - return self - - def __exit__(self, Exptype, expvalue, traceback): - if super(NfpHeatContextManager, self).__exit__( - Exptype, expvalue, traceback): - raise nfp_exceptions.HeatException( - Exptype, str(expvalue), traceback) - - # By default exit method returns False, if False is returned - # the with block re-raises the exception. To suppress that - # True should be returned explicitly - - return True - - -class NfpGBPContextManager(ContextManager): - - def new(self, **kwargs): - return NfpGBPContextManager(**kwargs) - - def __enter__(self): - super(NfpGBPContextManager, self).__enter__() - return self - - def __exit__(self, Exptype, expvalue, traceback): - if super(NfpGBPContextManager, self).__exit__( - Exptype, expvalue, traceback): - raise nfp_exceptions.GBPException( - Exptype, str(expvalue), traceback) - - # By default exit method returns False, if False is returned - # the with block re-raises the exception. To suppress that - # True should be returned explicitly - - return True - - -# Create the respective instances once, so that no need -# to instantiate them again any where - -DbContextManager = NfpDbContextManager() -NovaContextManager = NfpNovaContextManager() -KeystoneContextManager = NfpKeystoneContextManager() -NeutronContextManager = NfpNeutronContextManager() -HeatContextManager = NfpHeatContextManager() -GBPContextManager = NfpGBPContextManager() diff --git a/gbpservice/nfp/lib/nfp_exceptions.py b/gbpservice/nfp/lib/nfp_exceptions.py deleted file mode 100644 index 0fb047d07..000000000 --- a/gbpservice/nfp/lib/nfp_exceptions.py +++ /dev/null @@ -1,41 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - - -class GenericException(Exception): - - def __init__(self, type, value, traceback): - super(GenericException, self).__init__(type, value) - - -class DbException(GenericException): - pass - - -class NeutronException(GenericException): - pass - - -class NovaException(GenericException): - pass - - -class KeystoneException(GenericException): - pass - - -class GBPException(GenericException): - pass - - -class HeatException(GenericException): - pass diff --git a/gbpservice/nfp/lib/rest_client_over_unix.py b/gbpservice/nfp/lib/rest_client_over_unix.py deleted file mode 100644 index abb3beffe..000000000 --- a/gbpservice/nfp/lib/rest_client_over_unix.py +++ /dev/null @@ -1,171 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import http.client -import socket -import zlib - -import exceptions -import httplib2 -from oslo_serialization import jsonutils -import six.moves.urllib.parse as urlparse - -from gbpservice._i18n import _ -from gbpservice.nfp.core import log as nfp_logging - -LOG = nfp_logging.getLogger(__name__) - - -class RestClientException(exceptions.Exception): - - """ RestClient Exception """ - - -class UnixHTTPConnection(http.client.HTTPConnection): - - """Connection class for HTTP over UNIX domain socket.""" - - def __init__(self, host, port=None, strict=None, timeout=None, - proxy_info=None): - http.client.HTTPConnection.__init__(self, host, port, strict) - self.timeout = timeout - self.socket_path = '/var/run/uds_socket' - - def connect(self): - """Method used to connect socket server.""" - self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - if self.timeout: - self.sock.settimeout(self.timeout) - try: - self.sock.connect(self.socket_path) - except socket.error as exc: - raise RestClientException( - _("Caught exception socket.error : %s") % exc) - - -class UnixRestClient(object): - - def _http_request(self, url, method_type, headers=None, body=None): - try: - h = httplib2.Http() - resp, content = h.request( - url, - method=method_type, - headers=headers, - body=body, - connection_type=UnixHTTPConnection) - return resp, content - - except httplib2.ServerNotFoundError: - raise RestClientException(_("Server Not Found")) - - except exceptions.Exception as e: - raise RestClientException(_("httplib response error %s") % (e)) - - def send_request(self, path, method_type, request_method='http', - server_addr='127.0.0.1', - headers=None, body=None): - """Implementation for common interface for all unix crud requests. - Return:Http Response - """ - # prepares path, body, url for sending unix request. - if method_type.upper() != 'GET': - body = jsonutils.dumps(body) - body = zlib.compress(body) - - path = '/v1/nfp/' + path - url = urlparse.urlunsplit(( - request_method, - server_addr, - path, - None, - '')) - - try: - resp, content = self._http_request(url, method_type, - headers=headers, body=body) - if content != '': - content = zlib.decompress(content) - message = "%s:%s" % (resp, content) - LOG.info(message) - except RestClientException as rce: - message = "ERROR : %s" % (rce) - LOG.error(message) - raise rce - - success_code = [200, 201, 202, 204] - # Evaluate responses into success and failures. - # Raise exception for failure cases which needs - # to be handled by caller. - if success_code.__contains__(resp.status): - return resp, content - elif resp.status == 400: - raise RestClientException(_("HTTPBadRequest: %s") % resp.reason) - elif resp.status == 401: - raise RestClientException(_("HTTPUnauthorized: %s") % resp.reason) - elif resp.status == 403: - raise RestClientException(_("HTTPForbidden: %s") % resp.reason) - elif resp.status == 404: - raise RestClientException(_("HttpNotFound: %s") % resp.reason) - elif resp.status == 405: - raise RestClientException( - _("HTTPMethodNotAllowed: %s") % resp.reason) - elif resp.status == 406: - raise RestClientException(_("HTTPNotAcceptable: %s") % resp.reason) - elif resp.status == 408: - raise RestClientException( - _("HTTPRequestTimeout: %s") % resp.reason) - elif resp.status == 409: - raise RestClientException(_("HTTPConflict: %s") % resp.reason) - elif resp.status == 415: - raise RestClientException( - _("HTTPUnsupportedMediaType: %s") % resp.reason) - elif resp.status == 417: - raise RestClientException( - _("HTTPExpectationFailed: %s") % resp.reason) - elif resp.status == 500: - raise RestClientException(_("HTTPServerError: %s") % resp.reason) - else: - raise Exception(_('Unhandled Exception code: %(st)s %(reason)s') % - {'st': resp.status, 'reason': resp.reason}) - - -def get(path): - """Implements get method for unix restclient - Return:Http Response - """ - return UnixRestClient().send_request(path, 'GET') - - -def put(path, body): - """Implements put method for unix restclient - Return:Http Response - """ - headers = {'content-type': 'application/octet-stream'} - return UnixRestClient().send_request( - path, 'PUT', headers=headers, body=body) - - -def post(path, body, delete=False): - """Implements post method for unix restclient - Return:Http Response - """ - # Method-Type added here,as DELETE/CREATE - # both case are handled by post as delete also needs - # to send data to the rest-unix-server. - headers = {'content-type': 'application/octet-stream'} - if delete: - headers.update({'method-type': 'DELETE'}) - else: - headers.update({'method-type': 'CREATE'}) - return UnixRestClient().send_request( - path, 'POST', headers=headers, body=body) diff --git a/gbpservice/nfp/lib/transport.py b/gbpservice/nfp/lib/transport.py deleted file mode 100644 index 0db87a185..000000000 --- a/gbpservice/nfp/lib/transport.py +++ /dev/null @@ -1,353 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - - -import exceptions - -from neutron_lib import context as n_context -from neutron_lib import rpc as n_rpc -from oslo_config import cfg -from oslo_config import cfg as oslo_config -import oslo_messaging as messaging -from oslo_serialization import jsonutils -import requests -import six - -from gbpservice._i18n import _ -from gbpservice.nfp.common import constants as nfp_constants -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.lib import rest_client_over_unix as unix_rc - -LOG = nfp_logging.getLogger(__name__) -Version = 'v1' # v1/v2/v3# - -rest_opts = [ - cfg.StrOpt('rest_server_address', - default='', help='Rest connection IpAddr'), - cfg.IntOpt('rest_server_port', - default=8080, help='Rest connection Port'), -] - -rpc_opts = [ - cfg.StrOpt('topic', - default='', help='Topic for rpc connection'), -] - -oslo_config.CONF.register_opts(rest_opts, "REST") -oslo_config.CONF.register_opts(rpc_opts, "RPC") -n_rpc.init(cfg.CONF) - -UNIX_REST = 'unix_rest' -TCP_REST = 'tcp_rest' - -""" Common Class for restClient exceptions """ - - -class RestClientException(exceptions.Exception): - - """ RestClient Exception """ - - -""" Common Class to handle restclient request""" - - -class RestApi(object): - - def __init__(self, rest_server_address, rest_server_port): - self.rest_server_address = rest_server_address - self.rest_server_port = rest_server_port - self.url = "http://%s:%s/v1/nfp/%s" - - def _response(self, resp, url): - success_code = [200, 201, 202, 204] - # Evaluate responses into success and failures. - # Raise exception for failure cases which needs - # to be handled in caller function. - if success_code.__contains__(resp.status_code): - return resp - elif resp.status_code == 400: - raise RestClientException(_("HTTPBadRequest: %s") % resp.reason) - elif resp.status_code == 401: - raise RestClientException(_("HTTPUnauthorized: %s") % resp.reason) - elif resp.status_code == 403: - raise RestClientException(_("HTTPForbidden: %s") % resp.reason) - elif resp.status_code == 404: - raise RestClientException(_("HttpNotFound: %s") % resp.reason) - elif resp.status_code == 405: - raise RestClientException( - _("HTTPMethodNotAllowed: %s") % resp.reason) - elif resp.status_code == 406: - raise RestClientException(_("HTTPNotAcceptable: %s") % resp.reason) - elif resp.status_code == 408: - raise RestClientException( - _("HTTPRequestTimeout: %s") % resp.reason) - elif resp.status_code == 409: - raise RestClientException(_("HTTPConflict: %s") % resp.reason) - elif resp.status_code == 415: - raise RestClientException( - _("HTTPUnsupportedMediaType: %s") % resp.reason) - elif resp.status_code == 417: - raise RestClientException( - _("HTTPExpectationFailed: %s") % resp.reason) - elif resp.status_code == 500: - raise RestClientException(_("HTTPServerError: %s") % resp.reason) - else: - raise RestClientException(_('Unhandled Exception code: ' - '%(status)s %(reason)s') % - {'status': resp.status_code, - 'reason': resp.reason}) - return resp - - def post(self, path, body, method_type): - """Post restclient request handler - Return:Http response - """ - url = self.url % ( - self.rest_server_address, - self.rest_server_port, path) - data = jsonutils.dumps(body) - try: - # Method-Type needs to be added here,as DELETE/CREATE - # both case are handled by post as delete also needs - # to send data to the rest-server. - headers = {"content-type": "application/json", - "method-type": method_type} - resp = requests.post(url, data, - headers=headers) - message = "POST url %s %d" % (url, resp.status_code) - LOG.info(message) - return self._response(resp, url) - except RestClientException as rce: - message = "Rest API %s - Failed. Reason: %s" % ( - url, rce) - LOG.error(message) - - def put(self, path, body): - """Put restclient request handler - Return:Http response - """ - url = self.url % ( - self.rest_server_address, - self.rest_server_port, path) - data = jsonutils.dumps(body) - try: - headers = {"content-type": "application/json"} - resp = requests.put(url, data, - headers=headers) - message = "PUT url %s %d" % (url, resp.status_code) - LOG.info(message) - return self._response(resp, url) - except RestClientException as rce: - message = "Rest API %s - Failed. Reason: %s" % ( - url, rce) - LOG.error(message) - - def get(self, path): - """Get restclient request handler - Return:Http response - """ - url = self.url % ( - self.rest_server_address, - self.rest_server_port, path) - try: - headers = {"content-type": "application/json"} - resp = requests.get(url, - headers=headers) - message = "GET url %s %d" % (url, resp.status_code) - LOG.info(message) - return self._response(resp, url) - except RestClientException as rce: - message = "Rest API %s - Failed. Reason: %s" % ( - url, rce) - LOG.error(message) - - -""" Common Class to handle rpcclient request""" - - -class RPCClient(object): - API_VERSION = '1.0' - - def __init__(self, topic): - self.topic = topic - target = messaging.Target(topic=self.topic, - version=self.API_VERSION) - self.client = n_rpc.get_client(target) - self.cctxt = self.client.prepare(version=self.API_VERSION, - topic=self.topic) - - -def send_request_to_configurator(conf, context, body, - method_type, device_config=False, - network_function_event=False): - """Common function to handle (create, delete) request for configurator. - Send create/delete to configurator rest-server. - Return:Http Response - """ - # This function reads configuration data and decides - # method (tcp_rest/rpc) for sending request to configurator. - if device_config: - method_name = method_type.lower() + '_network_function_device_config' - body['info']['context'].update({'neutron_context': context.to_dict()}) - elif network_function_event: - method_name = 'network_function_event' - else: - if (body['config'][0]['resource'] in - list(nfp_constants.CONFIG_TAG_RESOURCE_MAP.values())): - body['config'][0]['resource_data'].update( - {'neutron_context': context.to_dict()}) - body['info']['context'].update( - {'neutron_context': context.to_dict()}) - method_name = method_type.lower() + '_network_function_config' - - if conf.backend == TCP_REST: - try: - rc = RestApi(conf.REST.rest_server_address, - conf.REST.rest_server_port) - if method_type.lower() in [nfp_constants.CREATE, - nfp_constants.DELETE]: - resp = rc.post(method_name, body, method_type.upper()) - message = "%s -> POST response: (%s) body: %s " % (method_name, - resp, body) - LOG.debug(message) - elif method_type.lower() in [nfp_constants.UPDATE]: - resp = rc.put(method_name, body) - message = "%s -> PUT response: (%s) body: %s " % (method_name, - resp, body) - LOG.debug(message) - else: - message = ("%s api not supported" % (method_name)) - LOG.error(message) - except RestClientException as rce: - message = "%s -> POST request failed.Reason: %s" % ( - method_name, rce) - LOG.error(message) - - elif conf.backend == UNIX_REST: - try: - if method_type.lower() in [nfp_constants.CREATE, - nfp_constants.DELETE]: - resp, content = unix_rc.post(method_name, - body=body) - message = ("%s -> POST response: (%s) body : %s " % - (method_name, content, body)) - LOG.debug(message) - elif method_type.lower() in [nfp_constants.UPDATE]: - resp, content = unix_rc.put(method_name, - body=body) - message = ("%s -> PUT response: (%s) body : %s " % - (method_name, content, body)) - LOG.debug(message) - else: - message = ("%s api not supported" % (method_name)) - LOG.error(message) - except unix_rc.RestClientException as rce: - message = "%s -> request failed . Reason %s " % ( - method_name, rce) - LOG.error(message) - - else: - message = ("%s -> RPC request sent. " % (method_name)) - LOG.info(message) - rpcClient = RPCClient(conf.RPC.topic) - rpcClient.cctxt.cast(context, method_name, - body=body) - - -def get_response_from_configurator(conf): - """Common function to handle get request for configurator. - Get notification http response from configurator rest server. - Return:Http Response - response_data = [ - {'receiver': , - 'resource': , - 'method': , - 'kwargs': - }, - ] - """ - # This function reads configuration data and decides - # method (tcp_rest/ unix_rest/ rpc) for get response from configurator. - if conf.backend == TCP_REST: - try: - rc = RestApi(conf.REST.rest_server_address, - conf.REST.rest_server_port) - resp = rc.get('get_notifications') - rpc_cbs_data = jsonutils.loads(resp.content) - return rpc_cbs_data - except RestClientException as rce: - message = ("get_notification ->" - "GET request failed. Reason : %s" % (rce)) - LOG.error(message) - return "get_notification -> GET request failed. Reason : %s" % ( - rce) - except Exception as e: - message = ("get_notification ->" - "GET request failed. Reason : %s" % (e)) - LOG.error(message) - return "get_notification -> GET request failed. Reason : %s" % ( - e) - - elif conf.backend == UNIX_REST: - try: - resp, content = unix_rc.get('get_notifications') - content = jsonutils.loads(content) - if content: - message = ("get_notification ->" - "GET response: (%s)" % (content)) - LOG.debug(message) - return content - except unix_rc.RestClientException as rce: - message = ("get_notification ->" - "GET request failed. Reason : %s" % ( - rce)) - LOG.error(message) - return "get_notification -> GET request failed. Reason : %s" % ( - rce) - except Exception as e: - message = ("get_notification ->" - "GET request failed. Reason : %s" % ( - e)) - LOG.error(message) - return "get_notification -> GET request failed. Reason : %s" % ( - e) - - else: - rpc_cbs_data = [] - try: - rpcClient = RPCClient(conf.RPC.topic) - context = n_context.Context( - 'config_agent_user', 'config_agent_tenant') - rpc_cbs_data = rpcClient.cctxt.call(context, - 'get_notifications') - return rpc_cbs_data - except Exception as e: - message = "Exception while processing %s" % e - LOG.error(message) - return "get_notification -> GET request failed. Reason : %s" % ( - e) - - -def parse_service_flavor_string(service_flavor_str): - """Parse service_flavour string to service details dictionary. - Return: Service Details Dictionary - """ - service_details = {} - if ',' not in service_flavor_str: - service_details['device_type'] = 'nova' - service_details['service_vendor'] = service_flavor_str - else: - service_flavor_dict = dict(item.split('=') for item - in service_flavor_str.split(',')) - service_details = {key.strip(): value.strip() for key, value - in six.iteritems(service_flavor_dict)} - return service_details diff --git a/gbpservice/nfp/orchestrator/coal/networking/__init__.py b/gbpservice/nfp/orchestrator/coal/networking/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/nfp/orchestrator/coal/networking/nfp_gbp_network_driver.py b/gbpservice/nfp/orchestrator/coal/networking/nfp_gbp_network_driver.py deleted file mode 100644 index 6a12c5ad6..000000000 --- a/gbpservice/nfp/orchestrator/coal/networking/nfp_gbp_network_driver.py +++ /dev/null @@ -1,93 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from gbpservice.nfp.lib import nfp_context_manager as nfp_ctx_mgr -from gbpservice.nfp.orchestrator.coal.networking import ( - nfp_neutron_network_driver as neutron_nd -) -from gbpservice.nfp.orchestrator.openstack import openstack_driver - - -class NFPGBPNetworkDriver(neutron_nd.NFPNeutronNetworkDriver): - - def __init__(self, config): - self.config = config - super(NFPGBPNetworkDriver, self).__init__(config) - self.network_handler = openstack_driver.GBPClient(config) - - def setup_traffic_steering(self): - pass - - def create_port(self, token, tenant_id, net_id, name=None): - with nfp_ctx_mgr.GBPContextManager as gcm: - port = gcm.retry(self.network_handler.create_policy_target, - token, tenant_id, net_id, name) - return port - - def delete_port(self, token, port_id): - with nfp_ctx_mgr.GBPContextManager as gcm: - gcm.retry( - self.network_handler.delete_policy_target, - token, port_id) - - def get_port_id(self, token, port_id): - with nfp_ctx_mgr.GBPContextManager as gcm: - pt = gcm.retry( - self.network_handler.get_policy_target, token, port_id) - return pt['port_id'] - - def update_port(self, token, port_id, port): - with nfp_ctx_mgr.GBPContextManager as gcm: - pt = gcm.retry( - self.network_handler.update_policy_target, - token, port_id, port) - return pt['port_id'] - - def get_neutron_port_details(self, token, port_id): - # self.network_handler = openstack_driver.NeutronClient(self.config) - port_details = ( - super(NFPGBPNetworkDriver, self).get_port_and_subnet_details( - token, port_id)) - # self.network_handler = openstack_driver.GBPClient(self.config) - return port_details - - def get_port_details(self, token, port_id): - _port_id = self.get_port_id(token, port_id) - # self.network_handler = openstack_driver.NeutronClient(self.config) - port_details = super(NFPGBPNetworkDriver, self).get_port_details( - token, _port_id) - # self.network_handler = openstack_driver.GBPClient(self.config) - return port_details - - def get_networks(self, token, filters): - with nfp_ctx_mgr.GBPContextManager as gcm: - return gcm.retry( - self.network_handler.get_policy_target_groups, - token, filters=filters) - - def set_promiscuos_mode(self, token, port_id, enable_port_security): - port_id = self.get_port_id(token, port_id) - # self.network_handler = openstack_driver.NeutronClient(self.config) - super(NFPGBPNetworkDriver, self).set_promiscuos_mode( - token, port_id, enable_port_security) - # self.network_handler = openstack_driver.GBPClient(self.config) - - def set_promiscuos_mode_fast(self, token, port_id, enable_port_security): - # self.network_handler = openstack_driver.NeutronClient(self.config) - super(NFPGBPNetworkDriver, self).set_promiscuos_mode( - token, port_id, enable_port_security) - # self.network_handler = openstack_driver.GBPClient(self.config) - - def get_service_profile(self, token, service_profile_id): - with nfp_ctx_mgr.GBPContextManager as gcm: - return gcm.retry(self.network_handler.get_service_profile, token, - service_profile_id) diff --git a/gbpservice/nfp/orchestrator/coal/networking/nfp_network_driver_base.py b/gbpservice/nfp/orchestrator/coal/networking/nfp_network_driver_base.py deleted file mode 100644 index 7893be0c0..000000000 --- a/gbpservice/nfp/orchestrator/coal/networking/nfp_network_driver_base.py +++ /dev/null @@ -1,39 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - - -class NFPNetworkDriverBase(object): - """ NFP Network Driver Base class - - Handles ports, operations on them - """ - - def __init__(self): - pass - - def setup_traffic_steering(self): - pass - - def create_port(self, token, admin_id, net_id, name=None): - pass - - def delete_port(self, token, port_id): - pass - - def get_port_id(self, token, port_id): - pass - - def get_port_details(self, token, port_id): - pass - - def set_promiscuos_mode(self, token, port_id, enable_port_security): - pass diff --git a/gbpservice/nfp/orchestrator/coal/networking/nfp_neutron_network_driver.py b/gbpservice/nfp/orchestrator/coal/networking/nfp_neutron_network_driver.py deleted file mode 100644 index 0570d0bfe..000000000 --- a/gbpservice/nfp/orchestrator/coal/networking/nfp_neutron_network_driver.py +++ /dev/null @@ -1,99 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from gbpservice.nfp.lib import nfp_context_manager as nfp_ctx_mgr -from gbpservice.nfp.orchestrator.coal.networking import ( - nfp_network_driver_base as ndb -) -from gbpservice.nfp.orchestrator.openstack import openstack_driver - - -class NFPNeutronNetworkDriver(ndb.NFPNetworkDriverBase): - - def __init__(self, config): - # self.network_handler = openstack_driver.NeutronClient(config) - self.neutron_client = openstack_driver.NeutronClient(config) - self.config = config - - def setup_traffic_steering(self): - pass - - def create_port(self, token, tenant_id, net_id, name=None): - with nfp_ctx_mgr.NeutronContextManager as ncm: - port = ncm.retry( - self.neutron_client.create_port, - token, tenant_id, net_id, - attrs={'name': name}) - return port - - def delete_port(self, token, port_id): - with nfp_ctx_mgr.NeutronContextManager as ncm: - ncm.retry(self.neutron_client.delete_port, token, port_id) - - def get_port_id(self, token, port_id): - return port_id - - def update_port(self, token, port_id, port): - with nfp_ctx_mgr.NeutronContextManager as ncm: - port = ncm.retry(self.neutron_client.update_port, - token, port_id, **port) - return port['port'] - - def get_port_and_subnet_details(self, token, port_id): - with nfp_ctx_mgr.NeutronContextManager as ncm: - port = ncm.retry(self.neutron_client.get_port, token, port_id) - - # ip - ip = port['port']['fixed_ips'][0]['ip_address'] - - # mac - mac = port['port']['mac_address'] - - # gateway ip - subnet_id = port['port']['fixed_ips'][0]['subnet_id'] - with nfp_ctx_mgr.NeutronContextManager as ncm: - subnet = ncm.retry( - self.neutron_client.get_subnet, token, subnet_id) - cidr = subnet['subnet']['cidr'] - gateway_ip = subnet['subnet']['gateway_ip'] - - return (ip, mac, cidr, gateway_ip, port, subnet) - - def get_port_details(self, token, port_id): - with nfp_ctx_mgr.NeutronContextManager as ncm: - port = ncm.retry(self.neutron_client.get_port, token, port_id) - - # ip - ip = port['port']['fixed_ips'][0]['ip_address'] - - # mac - mac = port['port']['mac_address'] - - # gateway ip - subnet_id = port['port']['fixed_ips'][0]['subnet_id'] - with nfp_ctx_mgr.NeutronContextManager as ncm: - subnet = ncm.retry( - self.neutron_client.get_subnet, token, subnet_id) - cidr = subnet['subnet']['cidr'] - gateway_ip = subnet['subnet']['gateway_ip'] - - return (ip, mac, cidr, gateway_ip, port, subnet) - - def set_promiscuos_mode(self, token, port_id, enable_port_security): - port_security = bool(enable_port_security) - with nfp_ctx_mgr.NeutronContextManager as ncm: - ncm.retry(self.neutron_client.update_port, token, port_id, - security_groups=[], - port_security_enabled=port_security) - - def get_service_profile(self, token, service_profile_id): - return {} diff --git a/gbpservice/nfp/orchestrator/config_drivers/heat_client.py b/gbpservice/nfp/orchestrator/config_drivers/heat_client.py deleted file mode 100644 index a2544cacb..000000000 --- a/gbpservice/nfp/orchestrator/config_drivers/heat_client.py +++ /dev/null @@ -1,70 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. -from heatclient import client as heat_client -from heatclient import exc as heat_exc - -from gbpservice.nfp.core import log as nfp_logging -LOG = nfp_logging.getLogger(__name__) - -# We are overriding create and update for now because the upstream -# heat client class does not take timeout as argument - - -class HeatClient(object): - - def __init__(self, user_name, tenant, heat_uri, password=None, - auth_token=None, timeout_mins=30): - api_version = "1" - endpoint = "%s/%s" % (heat_uri, tenant) - kwargs = { - 'token': auth_token, - 'username': user_name, - 'password': password - } - self.client = heat_client.Client(api_version, endpoint, **kwargs) - self.stacks = self.client.stacks - - self.timeout_mins = timeout_mins - # REVISIT(ashu): The base class is a old style class. We have to - # change when it is updated - # gbp_heat_api_client.HeatClient.__init__( - # self, context, heat_uri, password, auth_token) - - def create(self, name, data, parameters=None): - fields = { - 'stack_name': name, - 'timeout_mins': self.timeout_mins, - 'disable_rollback': True, - 'password': data.get('password') - } - fields['template'] = data - fields['parameters'] = parameters - return self.stacks.create(**fields) - - def update(self, stack_id, data, parameters=None): - fields = { - 'timeout_mins': self.timeout_mins, - 'password': data.get('password') - } - fields['template'] = data - fields['parameters'] = parameters - return self.stacks.update(stack_id, **fields) - - def delete(self, stack_id): - try: - self.stacks.delete(stack_id) - except heat_exc.HTTPNotFound: - LOG.warning("Stack %(stack)s created by service chain driver " - "is not found at cleanup", {'stack': stack_id}) - - def get(self, stack_id): - return self.stacks.get(stack_id) diff --git a/gbpservice/nfp/orchestrator/config_drivers/heat_driver.py b/gbpservice/nfp/orchestrator/config_drivers/heat_driver.py deleted file mode 100644 index 34d7dd438..000000000 --- a/gbpservice/nfp/orchestrator/config_drivers/heat_driver.py +++ /dev/null @@ -1,1906 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import ast -import copy -import time - -from heatclient import exc as heat_exc -from neutron_lib.plugins import constants as pconst -from oslo_config import cfg -from oslo_serialization import jsonutils -import yaml - -from gbpservice._i18n import _ -from gbpservice.neutron.db import api as db_api -from gbpservice.neutron.services.grouppolicy.common import constants as gconst -from gbpservice.neutron.services.servicechain.plugins.ncp import plumber_base -from gbpservice.nfp.common import constants as nfp_constants -from gbpservice.nfp.common import utils -from gbpservice.nfp.core import context as module_context -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.lib import nfp_context_manager as nfp_ctx_mgr -from gbpservice.nfp.lib import transport -from gbpservice.nfp.orchestrator.config_drivers.heat_client import HeatClient -from gbpservice.nfp.orchestrator.db import nfp_db as nfp_db -from gbpservice.nfp.orchestrator.openstack.openstack_driver import ( - KeystoneClient) -from gbpservice.nfp.orchestrator.openstack.openstack_driver import ( - NeutronClient) -from gbpservice.nfp.orchestrator.openstack.openstack_driver import GBPClient - - -HEAT_DRIVER_OPTS = [ - cfg.StrOpt('svc_management_ptg_name', - default='svc_management_ptg', - help=_("Name of the PTG that is associated with the " - "service management network")), - cfg.StrOpt('remote_vpn_client_pool_cidr', - default='192.168.254.0/24', - help=_("CIDR pool for remote vpn clients")), - cfg.StrOpt('heat_uri', - default='http://localhost:8004/v1', - help=_("Heat API server address to instantiate services " - "specified in the service chain.")), - cfg.IntOpt('stack_action_wait_time', - default=nfp_constants.STACK_ACTION_WAIT_TIME, - help=_("Seconds to wait for pending stack operation " - "to complete")), - cfg.BoolOpt('is_service_admin_owned', - help=_("Parameter to indicate whether the Service VM has to " - "to be owned by the Admin"), - default=True), - cfg.StrOpt('keystone_version', - default='v3', - help=_("Parameter to indicate version of keystone " - "used by heat_driver")), - cfg.StrOpt('internet_out_network_name', default=None, - help=_("Public external network name")), -] - -cfg.CONF.register_opts(HEAT_DRIVER_OPTS, - "heat_driver") - -SC_METADATA = ('{"sc_instance":"%s", "floating_ip": "%s", ' - '"provider_interface_mac": "%s", ' - '"network_function_id": "%s",' - '"service_vendor": "%s"}') - -SVC_MGMT_PTG_NAME = ( - cfg.CONF.heat_driver.svc_management_ptg_name) - -STACK_ACTION_WAIT_TIME = ( - cfg.CONF.heat_driver.stack_action_wait_time) -STACK_ACTION_RETRY_WAIT = 5 # Retry after every 5 seconds -APIC_OWNED_RES = 'apic_owned_res_' -INTERNET_OUT_EXT_NET_NAME = cfg.CONF.heat_driver.internet_out_network_name - -LOG = nfp_logging.getLogger(__name__) - - -class HeatDriver(object): - - def __init__(self, config): - self.keystoneclient = KeystoneClient(config) - self.gbp_client = GBPClient(config) - self.neutron_client = NeutronClient(config) - - self.keystone_conf = config.nfp_keystone_authtoken - keystone_version = self.keystone_conf.auth_version - with nfp_ctx_mgr.KeystoneContextManager as kcm: - self.v2client = kcm.retry( - self.keystoneclient._get_v2_keystone_admin_client, tries=3) - self.admin_id = self.v2client.users.find( - name=self.keystone_conf.admin_user).id - self.admin_role = self._get_role_by_name( - self.v2client, "admin", keystone_version) - self.heat_role = self._get_role_by_name( - self.v2client, "heat_stack_owner", keystone_version) - - def _resource_owner_tenant_id(self): - with nfp_ctx_mgr.KeystoneContextManager as kcm: - auth_token = kcm.retry( - self.keystoneclient.get_scoped_keystone_token, - self.keystone_conf.admin_user, - self.keystone_conf.admin_password, - self.keystone_conf.admin_tenant_name, tries=3) - tenant_id = kcm.retry( - self.keystoneclient.get_tenant_id, - auth_token, self.keystone_conf.admin_tenant_name, tries=3) - return tenant_id - - def _get_resource_owner_context(self): - if cfg.CONF.heat_driver.is_service_admin_owned: - tenant_id = self._resource_owner_tenant_id() - with nfp_ctx_mgr.KeystoneContextManager as kcm: - auth_token = kcm.retry( - self.keystoneclient.get_scoped_keystone_token, - self.keystone_conf.admin_user, - self.keystone_conf.admin_password, - self.keystone_conf.admin_tenant_name, - tenant_id, tries=3) - return auth_token, tenant_id - - def _get_role_by_name(self, keystone_client, name, keystone_version): - if keystone_version == 'v2.0': - roles = keystone_client.roles.list() - if roles: - for role in roles: - if name in role.name: - return role - else: - role = keystone_client.roles.list(name=name) - if role: - return role[0] - - def get_allocated_roles(self, v2client, user, tenant_id=None): - allocated_role_names = [] - allocated_roles = v2client.roles.roles_for_user(user, tenant=tenant_id) - if allocated_roles: - for role in allocated_roles: - allocated_role_names.append(role.name) - return allocated_role_names - - def _assign_admin_user_to_project_v2_keystone(self, project_id): - allocated_role_names = self.get_allocated_roles( - self.v2client, self.admin_id, project_id) - if self.admin_role: - if self.admin_role.name not in allocated_role_names: - self.v2client.roles.add_user_role( - self.admin_id, self.admin_role.id, tenant=project_id) - if self.heat_role: - if self.heat_role.name not in allocated_role_names: - self.v2client.roles.add_user_role(self.admin_id, - self.heat_role.id, - tenant=project_id) - - def _assign_admin_user_to_project(self, project_id): - keystone_version = self.keystone_conf.auth_version - - if keystone_version == 'v2.0': - return self._assign_admin_user_to_project_v2_keystone(project_id) - else: - with nfp_ctx_mgr.KeystoneContextManager as kcm: - v3client = kcm.retry( - self.keystoneclient._get_v3_keystone_admin_client, tries=3) - admin_id = v3client.users.find( - name=self.keystone_conf.admin_user).id - admin_role = self._get_role_by_name(v3client, "admin", - keystone_version) - if admin_role: - v3client.roles.grant(admin_role.id, user=admin_id, - project=project_id) - heat_role = self._get_role_by_name(v3client, "heat_stack_owner", - keystone_version) - if heat_role: - v3client.roles.grant(heat_role.id, user=admin_id, - project=project_id) - - def keystone(self, user, pwd, tenant_name, tenant_id=None): - if tenant_id: - with nfp_ctx_mgr.KeystoneContextManager as kcm: - return kcm.retry(self.keystoneclient.get_scoped_keystone_token, - user, pwd, tenant_name, tenant_id, tries=3) - else: - with nfp_ctx_mgr.KeystoneContextManager as kcm: - return kcm.retry(self.keystoneclient.get_scoped_keystone_token, - user, pwd, tenant_name, tries=3) - - def _get_heat_client(self, tenant_id, assign_admin=False): - # REVISIT(Akash) Need to discuss use cases why it is needed, - # since user can do it from ui also. hence, commenting it for now - ''' - if assign_admin: - try: - self._assign_admin_user_to_project(tenant_id) - except Exception: - LOG.exception("Failed to assign admin user to project") - return None - ''' - nfp_context = module_context.get() - auth_token = nfp_context['log_context']['auth_token'] - - timeout_mins, timeout_seconds = divmod(STACK_ACTION_WAIT_TIME, 60) - if timeout_seconds: - timeout_mins = timeout_mins + 1 - try: - heat_client = HeatClient( - self.keystone_conf.admin_user, - tenant_id, - cfg.CONF.heat_driver.heat_uri, - self.keystone_conf.admin_password, - auth_token=auth_token, - timeout_mins=timeout_mins) - except Exception: - LOG.exception("Failed to create heatclient object") - return None - - return heat_client - - def _get_tenant_context(self, tenant_id): - auth_token = self.keystone( - self.keystone_conf.admin_user, - self.keystone_conf.admin_password, - self.keystone_conf.admin_tenant_name, - tenant_id=tenant_id) - return auth_token, tenant_id - - def loadbalancer_post_stack_create(self, network_function_details): - db_handler = nfp_db.NFPDbBase() - db_session = db_api.get_writer_session() - service_details = self.get_service_details(network_function_details) - service_profile = service_details['service_profile'] - if service_profile['service_type'] == pconst.LOADBALANCERV2: - network_function_instance = network_function_details.get( - 'network_function_instance') - if network_function_instance: - for port in network_function_instance.get('port_info'): - with nfp_ctx_mgr.DbContextManager: - port_info = db_handler.get_port_info(db_session, - port) - if port_info['port_model'] != nfp_constants.GBP_PORT: - return - - def _post_stack_create(self, nfp_context): - return - - def _get_provider_ptg_info(self, auth_token, sci_id): - nfp_context = module_context.get() - with nfp_ctx_mgr.GBPContextManager: - servicechain_instance = nfp_context.get('service_chain_instance') - provider_ptg_id = servicechain_instance['provider_ptg_id'] - for ptg in nfp_context['provider']['ptg']: - if ptg['id'] == provider_ptg_id: - return ptg - - def _pre_stack_cleanup(self, network_function): - nfp_context = module_context.get() - auth_token = nfp_context['log_context']['auth_token'] - with nfp_ctx_mgr.GBPContextManager: - service_profile = self.gbp_client.get_service_profile( - auth_token, network_function['service_profile_id']) - - service_type = service_profile['service_type'] - service_details = transport.parse_service_flavor_string( - service_profile['service_flavor']) - base_mode_support = (True if service_details['device_type'] == 'None' - else False) - if (service_type == pconst.LOADBALANCERV2) and ( - not base_mode_support): - provider = self._get_provider_ptg_info( - auth_token, - network_function['service_chain_id']) - provider_tenant_id = provider['tenant_id'] - self._update_policy_targets_for_vip( - auth_token, provider_tenant_id, provider, service_type) - - def _post_stack_cleanup(self, network_function): - # TODO(ashu): In post stack cleanup, need to delete vip pt, currently - # we dont have any way to identify vip pt, so skipping this, but need - # to fix it. - return - - def _get_vip_pt(self, auth_token, vip_port_id): - vip_pt = None - filters = {'port_id': vip_port_id} - with nfp_ctx_mgr.GBPContextManager as gcm: - policy_targets = gcm.retry(self.gbp_client.get_policy_targets, - auth_token, - filters=filters) - if policy_targets: - vip_pt = policy_targets[0] - - return vip_pt - - def _get_lb_vip(self, auth_token, provider, service_type): - provider_subnet = None - lb_vip = None - lb_vip_name = None - - with nfp_ctx_mgr.NeutronContextManager as ncm: - provider_l2p_subnets = ncm.retry( - self.neutron_client.get_subnets, - auth_token, - filters={'id': provider['subnets']}) - for subnet in provider_l2p_subnets: - if not subnet['name'].startswith(APIC_OWNED_RES): - provider_subnet = subnet - break - if not provider_subnet: - LOG.error("Unable to get provider subnet for provider " - "policy target group %(provider_ptg)s", - {"provider_ptg": provider}) - return lb_vip, lb_vip_name - if service_type == pconst.LOADBALANCERV2: - with nfp_ctx_mgr.NeutronContextManager as ncm: - loadbalancers = ncm.retry( - self.neutron_client.get_loadbalancers, - auth_token, - filters={'vip_subnet_id': [provider_subnet['id']]}) - if loadbalancers: - loadbalancer = loadbalancers[0] - lb_vip = {} - lb_vip['ip_address'] = loadbalancer['vip_address'] - lb_vip['port_id'] = loadbalancer['vip_port_id'] - # lbaasv2 dont have vip resource, so considering loadbalancer - # id as vip_name - lb_vip_name = 'vip-' + loadbalancer['id'] - return lb_vip, lb_vip_name - - def _get_lb_service_targets(self, auth_token, provider): - nfp_context = module_context.get() - service_targets = [] - if 'delete' in nfp_context['log_context']['path']: - for policy_target in nfp_context['provider']['pt']: - if ('endpoint' in policy_target['name'] and - self._is_service_target(policy_target)): - service_targets.append(policy_target) - return service_targets - - if provider.get("policy_targets"): - filters = {'id': provider.get("policy_targets")} - else: - filters = {'policy_target_group_id': provider['id']} - with nfp_ctx_mgr.GBPContextManager as gcm: - policy_targets = gcm.retry(self.gbp_client.get_policy_targets, - auth_token, - filters=filters) - for policy_target in policy_targets: - if ('endpoint' in policy_target['name'] and - self._is_service_target(policy_target)): - service_targets.append(policy_target) - return service_targets - - def _update_policy_targets_for_vip(self, auth_token, - provider_tenant_id, - provider, service_type): - - with nfp_ctx_mgr.KeystoneContextManager as kcm: - admin_token = kcm.retry( - self.keystoneclient.get_admin_token, tries=3) - lb_vip, vip_name = self._get_lb_vip(auth_token, provider, service_type) - service_targets = self._get_lb_service_targets(admin_token, provider) - if not (lb_vip and service_targets): - return None - - def _get_provider_pt(self, auth_token, provider): - if provider.get("policy_targets"): - filters = {'id': provider.get("policy_targets")} - else: - filters = {'policy_target_group': provider['id']} - with nfp_ctx_mgr.GBPContextManager as gcm: - policy_targets = gcm.retry(self.gbp_client.get_policy_targets, - auth_token, - filters=filters) - for policy_target in policy_targets: - if ('endpoint' in policy_target['name'] and - self._is_service_target(policy_target)): - return policy_target - return None - - def _is_service_target(self, policy_target): - if policy_target['name'] and (policy_target['name'].startswith( - plumber_base.SERVICE_TARGET_NAME_PREFIX) or - policy_target['name'].startswith('tscp_endpoint_service') or - policy_target['name'].startswith('vip')): - return True - else: - return False - - def _get_member_ips(self, auth_token, ptg): - member_addresses = [] - if ptg.get("policy_targets"): - with nfp_ctx_mgr.GBPContextManager as gcm: - policy_targets = gcm.retry( - self.gbp_client.get_policy_targets, - auth_token, - filters={'id': ptg.get("policy_targets")}) - else: - return member_addresses - for policy_target in policy_targets: - if not self._is_service_target(policy_target): - port_id = policy_target.get("port_id") - if port_id: - with nfp_ctx_mgr.NeutronContextManager as ncm: - port = ncm.retry(self.neutron_client.get_port, - auth_token, port_id)['port'] - ip_address = port.get('fixed_ips')[0].get("ip_address") - member_addresses.append(ip_address) - return member_addresses - - def _generate_lbv2_member_template(self, is_template_aws_version, - member_ip, stack_template, - pool_name="pool"): - type_key = 'Type' if is_template_aws_version else 'type' - properties_key = ('Properties' if is_template_aws_version - else 'properties') - resources_key = 'Resources' if is_template_aws_version else 'resources' - res_key = 'Ref' if is_template_aws_version else 'get_resource' - - lbaas_loadbalancer_key = self._get_heat_resource_key( - stack_template[resources_key], - is_template_aws_version, - "OS::Neutron::LBaaS::LoadBalancer") - subnet = stack_template[resources_key][lbaas_loadbalancer_key][ - properties_key]['vip_subnet'] - - app_port = "app_port" - if stack_template[resources_key][pool_name].get("description"): - desc_dict = ast.literal_eval( - stack_template[resources_key][pool_name].get("description")) - if desc_dict.get("app_port_param_name"): - app_port = desc_dict.get("app_port_param_name") - - return {type_key: "OS::Neutron::LBaaS::PoolMember", - properties_key: { - "pool": {res_key: pool_name}, - "address": member_ip, - "protocol_port": {"get_param": app_port}, - "subnet": subnet, - "weight": 1}} - - def _modify_lbv2_resources_name(self, stack_template, provider_ptg, - is_template_aws_version): - pass - - def _generate_lbaasv2_pool_members(self, auth_token, stack_template, - config_param_values, provider_ptg, - is_template_aws_version): - resources_key = 'Resources' if is_template_aws_version else 'resources' - self._modify_lbv2_resources_name( - stack_template, provider_ptg, is_template_aws_version) - member_ips = self._get_member_ips(auth_token, provider_ptg) - if not member_ips: - return - pools = self._get_all_heat_resource_keys( - stack_template[resources_key], - is_template_aws_version, - "OS::Neutron::LBaaS::Pool" - ) - - healthmonitors = self._get_all_heat_resource_keys( - stack_template[resources_key], - is_template_aws_version, - "OS::Neutron::LBaaS::HealthMonitor" - ) - if not pools: - return - # Add "depends_on" to make sure resources get created sequentially. - # First member should be created after - # all pools and healthmonitors creation completed. - # Other members should be created one by one. - prev_member = None - pools_and_hms = [] + pools + healthmonitors - for pool in pools: - for member_ip in member_ips: - member_name = 'mem-' + member_ip + '-' + pool - member_template = ( - self._generate_lbv2_member_template( - is_template_aws_version, - member_ip, stack_template, pool_name=pool)) - if prev_member: - member_template.update({"depends_on": prev_member}) - # No previous member means it's the first member - else: - member_template.update({"depends_on": pools_and_hms}) - stack_template[resources_key][member_name] = member_template - prev_member = member_name - - def _get_consumers_for_chain(self, auth_token, provider): - filters = {'id': provider['provided_policy_rule_sets']} - with nfp_ctx_mgr.GBPContextManager as gcm: - provided_prs = gcm.retry(self.gbp_client.get_policy_rule_sets, - auth_token, filters=filters) - redirect_prs = None - for prs in provided_prs: - filters = {'id': prs['policy_rules']} - with nfp_ctx_mgr.GBPContextManager as gcm: - policy_rules = gcm.retry(self.gbp_client.get_policy_rules, - auth_token, filters=filters) - for policy_rule in policy_rules: - filters = {'id': policy_rule['policy_actions'], - 'action_type': [gconst.GP_ACTION_REDIRECT]} - with nfp_ctx_mgr.GBPContextManager as gcm: - policy_actions = gcm.retry( - self.gbp_client.get_policy_actions, - auth_token, filters=filters) - if policy_actions: - redirect_prs = prs - break - - if not redirect_prs: - LOG.error("Redirect rule doesn't exist in policy target rule " - " set") - return None, None - return (redirect_prs['consuming_policy_target_groups'], - redirect_prs['consuming_external_policies']) - - def _append_firewall_rule(self, stack_template, provider_cidr, - consumer_cidr, fw_template_properties, - consumer_id): - resources_key = fw_template_properties['resources_key'] - properties_key = fw_template_properties['properties_key'] - fw_rule_keys = fw_template_properties['fw_rule_keys'] - rule_name = "%s_%s" % ("node_driver_rule", consumer_id[:16]) - fw_policy_key = fw_template_properties['fw_policy_key'] - i = 1 - for fw_rule_key in fw_rule_keys: - fw_rule_name = (rule_name + '_' + str(i)) - stack_template[resources_key][fw_rule_name] = ( - copy.deepcopy(stack_template[resources_key][fw_rule_key])) - if not stack_template[resources_key][fw_rule_name][ - properties_key].get('destination_ip_address', None): - stack_template[resources_key][fw_rule_name][ - properties_key]['destination_ip_address'] = provider_cidr - # Use user provided Source for N-S - if consumer_cidr != "0.0.0.0/0": - if not stack_template[resources_key][fw_rule_name][ - properties_key].get('source_ip_address'): - stack_template[resources_key][fw_rule_name][ - properties_key]['source_ip_address'] = consumer_cidr - - if stack_template[resources_key][fw_policy_key][ - properties_key].get('firewall_rules'): - stack_template[resources_key][fw_policy_key][ - properties_key]['firewall_rules'].append({ - 'get_resource': fw_rule_name}) - i += 1 - - def _get_heat_resource_key(self, template_resource_dict, - is_template_aws_version, resource_name): - type_key = 'Type' if is_template_aws_version else 'type' - for key in template_resource_dict: - if template_resource_dict[key].get(type_key) == resource_name: - return key - - def _get_all_heat_resource_keys(self, template_resource_dict, - is_template_aws_version, resource_name): - type_key = 'Type' if is_template_aws_version else 'type' - resource_keys = [] - for key in template_resource_dict: - if template_resource_dict[key].get(type_key) == resource_name: - resource_keys.append(key) - return resource_keys - - def _create_firewall_template(self, auth_token, - service_details, stack_template): - - consuming_ptgs_details = service_details['consuming_ptgs_details'] - consumer_eps = service_details['consuming_external_policies'] - - # Handle a case where a chain is provided first and then consumed - # if (not consuming_ptgs_details) and (not consumer_eps): - # return None - - is_template_aws_version = stack_template.get( - 'AWSTemplateFormatVersion', False) - resources_key = 'Resources' if is_template_aws_version else 'resources' - properties_key = ('Properties' if is_template_aws_version - else 'properties') - fw_rule_keys = self._get_all_heat_resource_keys( - stack_template[resources_key], is_template_aws_version, - 'OS::Neutron::FirewallRule') - fw_policy_key = self._get_all_heat_resource_keys( - stack_template['resources'], is_template_aws_version, - 'OS::Neutron::FirewallPolicy')[0] - - provider_subnet = service_details['provider_subnet'] - provider_cidr = provider_subnet['cidr'] - - fw_template_properties = dict( - resources_key=resources_key, properties_key=properties_key, - is_template_aws_version=is_template_aws_version, - fw_rule_keys=fw_rule_keys, - fw_policy_key=fw_policy_key) - - for consumer in consuming_ptgs_details: - ptg = consumer['ptg'] - subnets = consumer['subnets'] - - # Skip the stitching PTG - if ptg['proxied_group_id']: - continue - - fw_template_properties.update({'name': ptg['id'][:3]}) - for subnet in subnets: - if subnet['name'].startswith(APIC_OWNED_RES): - continue - - consumer_cidr = subnet['cidr'] - self._append_firewall_rule(stack_template, - provider_cidr, consumer_cidr, - fw_template_properties, ptg['id']) - - for consumer_ep in consumer_eps: - fw_template_properties.update({'name': consumer_ep['id'][:3]}) - self._append_firewall_rule(stack_template, provider_cidr, - "0.0.0.0/0", fw_template_properties, - consumer_ep['id']) - - for rule_key in fw_rule_keys: - del stack_template[resources_key][rule_key] - stack_template[resources_key][fw_policy_key][ - properties_key]['firewall_rules'].remove( - {'get_resource': rule_key}) - - return stack_template - - def _update_firewall_template(self, auth_token, provider, stack_template): - consumer_ptgs, consumer_eps = self._get_consumers_for_chain( - auth_token, provider) - if (consumer_ptgs is None) and (consumer_eps is None): - return None - is_template_aws_version = stack_template.get( - 'AWSTemplateFormatVersion', False) - resources_key = 'Resources' if is_template_aws_version else 'resources' - properties_key = ('Properties' if is_template_aws_version - else 'properties') - fw_rule_keys = self._get_all_heat_resource_keys( - stack_template[resources_key], is_template_aws_version, - 'OS::Neutron::FirewallRule') - fw_policy_key = self._get_all_heat_resource_keys( - stack_template['resources'], is_template_aws_version, - 'OS::Neutron::FirewallPolicy')[0] - - with nfp_ctx_mgr.NeutronContextManager as ncm: - provider_l2p_subnets = ncm.retry( - self.neutron_client.get_subnets, - auth_token, - filters={'id': provider['subnets']}) - for subnet in provider_l2p_subnets: - if not subnet['name'].startswith(APIC_OWNED_RES): - provider_cidr = subnet['cidr'] - break - if not provider_cidr: - LOG.error("Unable to get provider cidr for provider " - "policy target group %(provider_ptg)s", - {"provider_ptg": provider}) - return None - - fw_template_properties = dict( - resources_key=resources_key, properties_key=properties_key, - is_template_aws_version=is_template_aws_version, - fw_rule_keys=fw_rule_keys, - fw_policy_key=fw_policy_key) - - if consumer_ptgs: - filters = {'id': consumer_ptgs} - with nfp_ctx_mgr.GBPContextManager as gcm: - consumer_ptgs_details = gcm.retry( - self.gbp_client.get_policy_target_groups, - auth_token, filters) - - # Revisit(Magesh): What is the name updated below ?? FW or Rule? - # This seems to have no effect in UTs - for consumer in consumer_ptgs_details: - if consumer['proxied_group_id']: - continue - fw_template_properties.update({'name': consumer['id'][:3]}) - for subnet_id in consumer['subnets']: - with nfp_ctx_mgr.NeutronContextManager as ncm: - subnet = ncm.retry(self.neutron_client.get_subnet, - auth_token, subnet_id)['subnet'] - if subnet['name'].startswith(APIC_OWNED_RES): - continue - - consumer_cidr = subnet['cidr'] - self._append_firewall_rule( - stack_template, provider_cidr, consumer_cidr, - fw_template_properties, consumer['id']) - - if consumer_eps: - filters = {'id': consumer_eps} - with nfp_ctx_mgr.GBPContextManager as gcm: - consumer_eps_details = gcm.retry( - self.gbp_client.get_external_policies, - auth_token, filters) - for consumer_ep in consumer_eps_details: - fw_template_properties.update({'name': consumer_ep['id'][:3]}) - self._append_firewall_rule(stack_template, provider_cidr, - "0.0.0.0/0", fw_template_properties, - consumer_ep['id']) - - for rule_key in fw_rule_keys: - del stack_template[resources_key][rule_key] - stack_template[resources_key][fw_policy_key][ - properties_key]['firewall_rules'].remove( - {'get_resource': rule_key}) - - return stack_template - - def _modify_fw_resources_name(self, stack_template, provider_ptg, - is_template_aws_version): - resources_key = 'Resources' if is_template_aws_version else 'resources' - properties_key = ('Properties' if is_template_aws_version - else 'properties') - resource_name = 'OS::Neutron::FirewallPolicy' - fw_policy_key = self._get_heat_resource_key( - stack_template[resources_key], - is_template_aws_version, - resource_name) - fw_resource_name = 'OS::Neutron::Firewall' - fw_key = self._get_heat_resource_key( - stack_template[resources_key], - is_template_aws_version, - fw_resource_name) - # Include provider name in firewall, firewall policy. - ptg_name = '-' + provider_ptg['name'] - stack_template[resources_key][fw_policy_key][ - properties_key]['name'] += ptg_name - stack_template[resources_key][fw_key][ - properties_key]['name'] += ptg_name - - def _get_management_gw_ip(self, auth_token): - filters = {'name': [SVC_MGMT_PTG_NAME]} - with nfp_ctx_mgr.GBPContextManager as gcm: - svc_mgmt_ptgs = gcm.retry(self.gbp_client.get_policy_target_groups, - auth_token, filters) - if not svc_mgmt_ptgs: - LOG.error("Service Management Group is not created by Admin") - return None - else: - mgmt_subnet_id = svc_mgmt_ptgs[0]['subnets'][0] - with nfp_ctx_mgr.NeutronContextManager as ncm: - mgmt_subnet = ncm.retry(self.neutron_client.get_subnet, - auth_token, mgmt_subnet_id)['subnet'] - mgmt_gw_ip = mgmt_subnet['gateway_ip'] - return mgmt_gw_ip - - def _get_site_conn_keys(self, template_resource_dict, - is_template_aws_version, resource_name): - keys = [] - type_key = 'Type' if is_template_aws_version else 'type' - for key in template_resource_dict: - if template_resource_dict[key].get(type_key) == resource_name: - keys.append(key) - return keys - - def _get_resource_desc(self, nfp_context, service_details): - # This function prepares the description corresponding to service_type - # with required parameters, which NCO sends to NFP controller - device_type = service_details['service_details']['device_type'] - base_mode_support = (True if device_type == 'None' - else False) - - network_function_id = nfp_context['network_function']['id'] - # service_profile = service_details['service_profile'] - service_chain_instance_id = service_details[ - 'servicechain_instance']['id'] - consumer_port = service_details['consumer_port'] - provider_port = service_details['provider_port'] - mgmt_ip = service_details['mgmt_ip'] - - auth_token = nfp_context['resource_owner_context']['admin_token'] - tenant_id = nfp_context['tenant_id'] - - service_type = service_details['service_details']['service_type'] - service_vendor = service_details['service_details']['service_vendor'] - nf_desc = '' - - if not base_mode_support: - provider_port_mac = provider_port['mac_address'] - provider_cidr = service_details['provider_subnet']['cidr'] - else: - return - - if service_type == pconst.LOADBALANCERV2: - nf_desc = str((SC_METADATA % (service_chain_instance_id, - mgmt_ip, - provider_port_mac, - network_function_id, - service_vendor))) - elif service_type == pconst.FIREWALL: - firewall_desc = {'vm_management_ip': mgmt_ip, - 'provider_ptg_info': [provider_port_mac], - 'provider_cidr': provider_cidr, - 'service_vendor': service_vendor, - 'network_function_id': network_function_id} - nf_desc = str(firewall_desc) - elif service_type == pconst.VPN: - stitching_cidr = service_details['consumer_subnet']['cidr'] - mgmt_gw_ip = self._get_management_gw_ip(auth_token) - if not mgmt_gw_ip: - return None - - with nfp_ctx_mgr.GBPContextManager as gcm: - services_nsp = gcm.retry( - self.gbp_client.get_network_service_policies, - auth_token, - filters={'name': ['nfp_services_nsp']}) - if not services_nsp: - fip_nsp = { - 'network_service_policy': { - 'name': 'nfp_services_nsp', - 'description': 'nfp_implicit_resource', - 'shared': False, - 'tenant_id': tenant_id, - 'network_service_params': [ - {"type": "ip_pool", "value": "nat_pool", - "name": "vpn_svc_external_access"}] - } - } - with nfp_ctx_mgr.GBPContextManager as gcm: - nsp = gcm.retry( - self.gbp_client.create_network_service_policy, - auth_token, fip_nsp) - else: - nsp = services_nsp[0] - - with nfp_ctx_mgr.GBPContextManager as gcm: - stitching_pts = gcm.retry( - self.gbp_client.get_policy_targets, - auth_token, - filters={'port_id': [consumer_port['id']]}) - if not stitching_pts: - LOG.error("Policy target is not created for the " - "stitching port") - return None - stitching_ptg_id = ( - stitching_pts[0]['policy_target_group_id']) - - with nfp_ctx_mgr.GBPContextManager as gcm: - gcm.retry(self.gbp_client.update_policy_target_group, - auth_token, stitching_ptg_id, - {'policy_target_group': { - 'network_service_policy_id': nsp['id']}}) - - stitching_port_fip = self._get_consumer_fip(auth_token, - consumer_port) - if not stitching_port_fip: - return None - desc = ('fip=' + mgmt_ip + - ";tunnel_local_cidr=" + - provider_cidr + ";user_access_ip=" + - stitching_port_fip + ";fixed_ip=" + - consumer_port['fixed_ips'][0]['ip_address'] + - ';service_vendor=' + service_vendor + - ';stitching_cidr=' + stitching_cidr + - ';stitching_gateway=' + service_details[ - 'consumer_subnet']['gateway_ip'] + - ';mgmt_gw_ip=' + mgmt_gw_ip + - ';network_function_id=' + network_function_id) - nf_desc = str(desc) - - return nf_desc - - def get_neutron_resource_description(self, nfp_context): - service_details = self.get_service_details_from_nfp_context( - nfp_context) - - nf_desc = self._get_resource_desc(nfp_context, service_details) - return nf_desc - - def _create_node_config_data(self, auth_token, tenant_id, - service_chain_node, service_chain_instance, - provider, provider_port, consumer, - consumer_port, network_function, - mgmt_ip, service_details): - - common_desc = {'network_function_id': str(network_function['id'])} - - service_type = service_details['service_details']['service_type'] - device_type = service_details['service_details']['device_type'] - base_mode_support = (True if device_type == 'None' - else False) - - _, stack_template_str = self.parse_template_config_string( - service_chain_node.get('config')) - try: - stack_template = (jsonutils.loads(stack_template_str) if - stack_template_str.startswith('{') else - yaml.load(stack_template_str)) - except Exception: - LOG.error( - "Unable to load stack template for service chain " - "node: %(node_id)s", {'node_id': service_chain_node}) - return None, None - config_param_values = service_chain_instance.get( - 'config_param_values', '{}') - stack_params = {} - try: - config_param_values = jsonutils.loads(config_param_values) - except Exception: - LOG.error("Unable to load config parameters") - return None, None - - is_template_aws_version = stack_template.get( - 'AWSTemplateFormatVersion', False) - resources_key = ('Resources' if is_template_aws_version - else 'resources') - parameters_key = ('Parameters' if is_template_aws_version - else 'parameters') - properties_key = ('Properties' if is_template_aws_version - else 'properties') - - if not base_mode_support: - provider_subnet = service_details['provider_subnet'] - - if service_type == pconst.LOADBALANCERV2: - self._generate_lbaasv2_pool_members( - auth_token, stack_template, config_param_values, - provider, is_template_aws_version) - config_param_values['Subnet'] = provider_subnet['id'] - config_param_values['service_chain_metadata'] = "" - if not base_mode_support: - config_param_values[ - 'service_chain_metadata'] = str(common_desc) - - lb_loadbalancer_key = self._get_heat_resource_key( - stack_template[resources_key], - is_template_aws_version, - 'OS::Neutron::LBaaS::LoadBalancer') - stack_template[resources_key][lb_loadbalancer_key][ - properties_key]['description'] = str(common_desc) - - elif service_type == pconst.FIREWALL: - stack_template = self._create_firewall_template( - auth_token, service_details, stack_template) - - if not stack_template: - return None, None - self._modify_fw_resources_name( - stack_template, provider, is_template_aws_version) - if not base_mode_support: - - fw_key = self._get_heat_resource_key( - stack_template[resources_key], - is_template_aws_version, - 'OS::Neutron::Firewall') - stack_template[resources_key][fw_key][properties_key][ - 'description'] = str(common_desc) - elif service_type == pconst.VPN: - config_param_values['Subnet'] = ( - provider_port['fixed_ips'][0]['subnet_id'] - if consumer_port else None) - with nfp_ctx_mgr.GBPContextManager as gcm: - l2p = gcm.retry(self.gbp_client.get_l2_policy, - auth_token, provider['l2_policy_id']) - l3p = gcm.retry(self.gbp_client.get_l3_policy, - auth_token, l2p['l3_policy_id']) - config_param_values['RouterId'] = l3p['routers'][0] - mgmt_gw_ip = self._get_management_gw_ip(auth_token) - if not mgmt_gw_ip: - return None, None - - with nfp_ctx_mgr.NeutronContextManager as ncm: - provider_cidr = ncm.retry( - self.neutron_client.get_subnet, - auth_token, provider_port['fixed_ips'][0][ - 'subnet_id'])['subnet']['cidr'] - provider_cidr = provider_cidr - stitching_port_fip = self._get_consumer_fip(auth_token, - consumer_port) - if not stitching_port_fip: - return None, None - if not base_mode_support: - # stack_params['ServiceDescription'] = nf_desc - siteconn_keys = self._get_site_conn_keys( - stack_template[resources_key], - is_template_aws_version, - 'OS::Neutron::IPsecSiteConnection') - for siteconn_key in siteconn_keys: - stack_template[resources_key][siteconn_key][ - properties_key]['description'] = str(common_desc) - - vpnservice_key = self._get_heat_resource_key( - stack_template[resources_key], - is_template_aws_version, - 'OS::Neutron::VPNService') - vpn_description, _ = ( - utils.get_vpn_description_from_nf(network_function)) - vpnsvc_desc = {'fip': vpn_description['user_access_ip'], - 'ip': vpn_description['fixed_ip'], - 'cidr': vpn_description['tunnel_local_cidr']} - vpnsvc_desc.update(common_desc) - stack_template[resources_key][vpnservice_key][properties_key][ - 'description'] = str(vpnsvc_desc) - - for parameter in stack_template.get(parameters_key) or []: - if parameter in config_param_values: - stack_params[parameter] = config_param_values[parameter] - - LOG.info('Final stack_template : %(stack_data)s, ' - 'stack_params : %(params)s', - {'stack_data': stack_template, 'params': stack_params}) - return (stack_template, stack_params) - - def _get_consumer_fip(self, token, consumer_port): - with nfp_ctx_mgr.NeutronContextManager as ncm: - ext_net = ncm.retry( - self.neutron_client.get_networks, - token, filters={'name': [INTERNET_OUT_EXT_NET_NAME]}) - if not ext_net: - LOG.error("'internet_out_network_name' not configured" - " in [heat_driver] or Network %(network)s is" - " not found", - {'network': INTERNET_OUT_EXT_NET_NAME}) - return None - # There is a case where consumer port has multiple fips - filters = {'port_id': [consumer_port['id']], - 'floating_network_id': [ext_net[0]['id']]} - try: - # return floatingip of the stitching port -> consumer_port['id'] - with nfp_ctx_mgr.NeutronContextManager as ncm: - return ncm.retry(self.neutron_client.get_floating_ips, token, - **filters)[0]['floating_ip_address'] - except Exception: - LOG.error("Floating IP for VPN Service has either exhausted" - " or has been disassociated Manually") - return None - - def _update_node_config(self, auth_token, tenant_id, service_profile, - service_chain_node, service_chain_instance, - provider, consumer_port, network_function, - provider_port, update=False, mgmt_ip=None, - consumer=None): - nf_desc = None - common_desc = {'network_function_id': str(network_function['id'])} - provider_cidr = provider_subnet = None - with nfp_ctx_mgr.NeutronContextManager as ncm: - provider_l2p_subnets = ncm.retry( - self.neutron_client.get_subnets, - auth_token, filters={'id': provider['subnets']}) - for subnet in provider_l2p_subnets: - if not subnet['name'].startswith(APIC_OWNED_RES): - provider_cidr = subnet['cidr'] - provider_subnet = subnet - break - if not provider_cidr: - LOG.error("No provider cidr availabale") - return None, None - service_type = service_profile['service_type'] - service_details = transport.parse_service_flavor_string( - service_profile['service_flavor']) - base_mode_support = (True if service_details['device_type'] == 'None' - else False) - - _, stack_template_str = self.parse_template_config_string( - service_chain_node.get('config')) - try: - stack_template = (jsonutils.loads(stack_template_str) if - stack_template_str.startswith('{') else - yaml.load(stack_template_str)) - except Exception: - LOG.error( - "Unable to load stack template for service chain " - "node: %(node_id)s", {'node_id': service_chain_node}) - return None, None - config_param_values = service_chain_instance.get( - 'config_param_values', '{}') - stack_params = {} - try: - config_param_values = jsonutils.loads(config_param_values) - except Exception: - LOG.error("Unable to load config parameters") - return None, None - - is_template_aws_version = stack_template.get( - 'AWSTemplateFormatVersion', False) - resources_key = ('Resources' if is_template_aws_version - else 'resources') - parameters_key = ('Parameters' if is_template_aws_version - else 'parameters') - properties_key = ('Properties' if is_template_aws_version - else 'properties') - - if not base_mode_support: - provider_port_mac = provider_port['mac_address'] - with nfp_ctx_mgr.NeutronContextManager as ncm: - provider_cidr = ncm.retry( - self.neutron_client.get_subnet, - auth_token, provider_port['fixed_ips'][0][ - 'subnet_id'])['subnet']['cidr'] - else: - provider_port_mac = '' - provider_cidr = '' - - service_vendor = service_details['service_vendor'] - if service_type == pconst.LOADBALANCERV2: - self._generate_lbaasv2_pool_members( - auth_token, stack_template, config_param_values, - provider, is_template_aws_version) - config_param_values['Subnet'] = provider_subnet['id'] - config_param_values['service_chain_metadata'] = "" - if not base_mode_support: - config_param_values[ - 'service_chain_metadata'] = str(common_desc) - nf_desc = str((SC_METADATA % (service_chain_instance['id'], - mgmt_ip, - provider_port_mac, - network_function['id'], - service_vendor))) - - lb_loadbalancer_key = self._get_heat_resource_key( - stack_template[resources_key], - is_template_aws_version, - 'OS::Neutron::LBaaS::LoadBalancer') - stack_template[resources_key][lb_loadbalancer_key][ - properties_key]['description'] = str(common_desc) - - elif service_type == pconst.FIREWALL: - stack_template = self._update_firewall_template( - auth_token, provider, stack_template) - if not stack_template: - return None, None - self._modify_fw_resources_name( - stack_template, provider, is_template_aws_version) - if not base_mode_support: - firewall_desc = {'vm_management_ip': mgmt_ip, - 'provider_ptg_info': [provider_port_mac], - 'provider_cidr': provider_cidr, - 'service_vendor': service_vendor, - 'network_function_id': network_function[ - 'id']} - - fw_key = self._get_heat_resource_key( - stack_template[resources_key], - is_template_aws_version, - 'OS::Neutron::Firewall') - stack_template[resources_key][fw_key][properties_key][ - 'description'] = str(common_desc) - - nf_desc = str(firewall_desc) - elif service_type == pconst.VPN: - config_param_values['Subnet'] = ( - provider_port['fixed_ips'][0]['subnet_id'] - if consumer_port else None) - with nfp_ctx_mgr.GBPContextManager as gcm: - l2p = gcm.retry(self.gbp_client.get_l2_policy, - auth_token, provider['l2_policy_id']) - l3p = gcm.retry(self.gbp_client.get_l3_policy, - auth_token, l2p['l3_policy_id']) - config_param_values['RouterId'] = l3p['routers'][0] - with nfp_ctx_mgr.NeutronContextManager as ncm: - stitching_subnet = ncm.retry(self.neutron_client.get_subnet, - auth_token, - consumer['subnets'][0])['subnet'] - stitching_cidr = stitching_subnet['cidr'] - mgmt_gw_ip = self._get_management_gw_ip(auth_token) - if not mgmt_gw_ip: - return None, None - if not update: - with nfp_ctx_mgr.GBPContextManager as gcm: - services_nsp = gcm.retry( - self.gbp_client.get_network_service_policies, - auth_token, - filters={'name': ['nfp_services_nsp']}) - if not services_nsp: - fip_nsp = { - 'network_service_policy': { - 'name': 'nfp_services_nsp', - 'description': 'nfp_implicit_resource', - 'shared': False, - 'tenant_id': tenant_id, - 'network_service_params': [ - {"type": "ip_pool", "value": "nat_pool", - "name": "vpn_svc_external_access"}] - } - } - with nfp_ctx_mgr.GBPContextManager as gcm: - nsp = gcm.retry( - self.gbp_client.create_network_service_policy, - auth_token, fip_nsp) - else: - nsp = services_nsp[0] - if not base_mode_support: - with nfp_ctx_mgr.GBPContextManager as gcm: - stitching_pts = gcm.retry( - self.gbp_client.get_policy_targets, - auth_token, - filters={'port_id': [consumer_port['id']]}) - if not stitching_pts: - LOG.error("Policy target is not created for the " - "stitching port") - return None, None - stitching_ptg_id = ( - stitching_pts[0]['policy_target_group_id']) - else: - stitching_ptg_id = consumer['id'] - with nfp_ctx_mgr.GBPContextManager as gcm: - gcm.retry(self.gbp_client.update_policy_target_group, - auth_token, stitching_ptg_id, - {'policy_target_group': { - 'network_service_policy_id': nsp['id']}}) - if not base_mode_support: - with nfp_ctx_mgr.NeutronContextManager as ncm: - ext_net = ncm.retry( - self.neutron_client.get_networks, - auth_token, - filters={'name': [INTERNET_OUT_EXT_NET_NAME]}) - if not ext_net: - LOG.error("'internet_out_network_name' not configured" - " in [heat_driver] or Network %(network)s is" - " not found", - {'network': INTERNET_OUT_EXT_NET_NAME}) - return None, None - filters = {'port_id': [consumer_port['id']], - 'floating_network_id': [ext_net[0]['id']]} - with nfp_ctx_mgr.NeutronContextManager as ncm: - floatingips = ncm.retry( - self.neutron_client.get_floating_ips, - auth_token, filters=filters) - if not floatingips: - LOG.error("Floating IP for VPN Service has been " - "disassociated Manually") - return None, None - for fip in floatingips: - if consumer_port['fixed_ips'][0]['ip_address'] == fip[ - 'fixed_ip_address']: - stitching_port_fip = fip['floating_ip_address'] - - try: - desc = ('fip=' + mgmt_ip + - ";tunnel_local_cidr=" + - provider_cidr + ";user_access_ip=" + - stitching_port_fip + ";fixed_ip=" + - consumer_port['fixed_ips'][0]['ip_address'] + - ';service_vendor=' + service_details[ - 'service_vendor'] + - ';stitching_cidr=' + stitching_cidr + - ';stitching_gateway=' + stitching_subnet[ - 'gateway_ip'] + - ';mgmt_gw_ip=' + mgmt_gw_ip + - ';network_function_id=' + network_function['id']) - except Exception as e: - LOG.error("Problem in preparing description, some of " - "the fields might not have initialized. " - "Error: %(error)s", {'error': e}) - return None, None - siteconn_keys = self._get_site_conn_keys( - stack_template[resources_key], - is_template_aws_version, - 'OS::Neutron::IPsecSiteConnection') - for siteconn_key in siteconn_keys: - stack_template[resources_key][siteconn_key][ - properties_key]['description'] = str(common_desc) - - vpnservice_key = self._get_heat_resource_key( - stack_template[resources_key], - is_template_aws_version, - 'OS::Neutron::VPNService') - vpn_description, _ = ( - utils.get_vpn_description_from_nf(network_function)) - vpnsvc_desc = {'fip': vpn_description['user_access_ip'], - 'ip': vpn_description['fixed_ip'], - 'cidr': vpn_description['tunnel_local_cidr']} - vpnsvc_desc.update(common_desc) - stack_template[resources_key][vpnservice_key][properties_key][ - 'description'] = str(vpnsvc_desc) - nf_desc = str(desc) - - if nf_desc: - network_function['description'] = network_function[ - 'description'] + '\n' + nf_desc - - for parameter in stack_template.get(parameters_key) or []: - if parameter in config_param_values: - stack_params[parameter] = config_param_values[parameter] - - LOG.info('Final stack_template : %(stack_data)s, ' - 'stack_params : %(params)s', - {'stack_data': stack_template, 'params': stack_params}) - return (stack_template, stack_params) - - def parse_template_config_string(self, config_str): - service_config = tag_str = '' - for tag_str in [nfp_constants.HEAT_CONFIG_TAG, - nfp_constants.CONFIG_INIT_TAG, - nfp_constants.ANSIBLE_TAG, - nfp_constants.CUSTOM_JSON]: - try: - service_config = config_str.split(tag_str + ':')[1] - break - except IndexError: - # Try for next tag - pass - except Exception: - return None, None - if not service_config: - service_config = config_str - tag_str = nfp_constants.HEAT_CONFIG_TAG - return tag_str, service_config - - def get_service_details(self, network_function_details): - db_handler = nfp_db.NFPDbBase() - db_session = db_api.get_writer_session() - network_function = network_function_details['network_function'] - network_function_instance = network_function_details.get( - 'network_function_instance') - service_profile_id = network_function['service_profile_id'] - with nfp_ctx_mgr.KeystoneContextManager as kcm: - admin_token = kcm.retry( - self.keystoneclient.get_admin_token, tries=3) - with nfp_ctx_mgr.GBPContextManager as gcm: - service_profile = gcm.retry(self.gbp_client.get_service_profile, - admin_token, service_profile_id) - - service_details = transport.parse_service_flavor_string( - service_profile['service_flavor']) - if service_details['device_type'] != 'None': - network_function_device = network_function_details[ - 'network_function_device'] - mgmt_ip = network_function_device['mgmt_ip_address'] - else: - mgmt_ip = None - - config_policy_id = network_function['config_policy_id'] - service_id = network_function['service_id'] - with nfp_ctx_mgr.GBPContextManager as gcm: - servicechain_node = gcm.retry( - self.gbp_client.get_servicechain_node, - admin_token, service_id) - service_chain_id = network_function['service_chain_id'] - servicechain_instance = gcm.retry( - self.gbp_client.get_servicechain_instance, - admin_token, - service_chain_id) - provider_ptg_id = servicechain_instance['provider_ptg_id'] - consumer_ptg_id = servicechain_instance['consumer_ptg_id'] - provider_ptg = gcm.retry(self.gbp_client.get_policy_target_group, - admin_token, - provider_ptg_id) - consumer_ptg = None - if consumer_ptg_id and consumer_ptg_id != 'N/A': - consumer_ptg = gcm.retry( - self.gbp_client.get_policy_target_group, - admin_token, - consumer_ptg_id) - - consumer_port = None - provider_port = None - consumer_policy_target_group = None - provider_policy_target_group = None - policy_target = None - if network_function_instance: - for port in network_function_instance.get('port_info'): - with nfp_ctx_mgr.DbContextManager: - port_info = db_handler.get_port_info(db_session, port) - port_classification = port_info['port_classification'] - if port_info['port_model'] == nfp_constants.GBP_PORT: - policy_target_id = port_info['id'] - with nfp_ctx_mgr.GBPContextManager as gcm: - port_id = gcm.retry( - self.gbp_client.get_policy_targets, - admin_token, - filters={'id': policy_target_id})[0]['port_id'] - policy_target = gcm.retry( - self.gbp_client.get_policy_target, - admin_token, policy_target_id) - else: - port_id = port_info['id'] - - if port_classification == nfp_constants.CONSUMER: - with nfp_ctx_mgr.NeutronContextManager as ncm: - consumer_port = ncm.retry(self.neutron_client.get_port, - admin_token, port_id)['port'] - if policy_target: - with nfp_ctx_mgr.GBPContextManager as gcm: - consumer_policy_target_group = ( - gcm.retry( - self.gbp_client.get_policy_target_group, - admin_token, - policy_target['policy_target_group_id'])) - elif port_classification == nfp_constants.PROVIDER: - LOG.info("provider info: %(p_info)s", - {'p_info': port_id}) - with nfp_ctx_mgr.NeutronContextManager as ncm: - provider_port = ncm.retry(self.neutron_client.get_port, - admin_token, port_id)['port'] - if policy_target: - with nfp_ctx_mgr.GBPContextManager as gcm: - provider_policy_target_group = ( - gcm.retry( - self.gbp_client.get_policy_target_group, - admin_token, - policy_target['policy_target_group_id'])) - - service_details = { - 'service_profile': service_profile, - 'servicechain_node': servicechain_node, - 'servicechain_instance': servicechain_instance, - 'consumer_port': consumer_port, - 'provider_port': provider_port, - 'mgmt_ip': mgmt_ip, - 'policy_target_group': provider_policy_target_group, - 'config_policy_id': config_policy_id, - 'provider_ptg': provider_ptg, - 'consumer_ptg': consumer_ptg or consumer_policy_target_group - } - - return service_details - - def _wait_for_stack_operation_complete(self, heatclient, stack_id, action, - ignore_error=False): - time_waited = 0 - operation_failed = False - timeout_mins, timeout_seconds = divmod(STACK_ACTION_WAIT_TIME, 60) - if timeout_seconds: - timeout_mins = timeout_mins + 1 - # Heat timeout is in order of minutes. Allow Node driver to wait a - # little longer than heat timeout - wait_timeout = timeout_mins * 60 + 30 - while True: - try: - stack = heatclient.get(stack_id) - if stack.stack_status == 'DELETE_FAILED': - heatclient.delete(stack_id) - elif stack.stack_status == 'CREATE_COMPLETE': - return - elif stack.stack_status == 'DELETE_COMPLETE': - LOG.info("Stack %(stack)s is deleted", - {'stack': stack_id}) - if action == "delete": - return - else: - operation_failed = True - elif stack.stack_status == 'CREATE_FAILED': - operation_failed = True - elif stack.stack_status == 'UPDATE_FAILED': - operation_failed = True - elif stack.stack_status not in [ - 'UPDATE_IN_PROGRESS', 'CREATE_IN_PROGRESS', - 'DELETE_IN_PROGRESS']: - return - except heat_exc.HTTPNotFound: - LOG.warning( - "Stack %(stack)s created by service chain " - "driver is not found while waiting for %(action)s " - "to complete", - {'stack': stack_id, 'action': action}) - if action == "create" or action == "update": - operation_failed = True - else: - return - except Exception: - LOG.exception("Retrieving the stack %(stack)s failed.", - {'stack': stack_id}) - if action == "create" or action == "update": - operation_failed = True - else: - return - - if operation_failed: - if ignore_error: - return - else: - LOG.error("Stack %(stack_name)s %(action)s failed for " - "tenant %(stack_owner)s", - {'stack_name': stack.stack_name, - 'stack_owner': stack.stack_owner, - 'action': action}) - return None - else: - time.sleep(STACK_ACTION_RETRY_WAIT) - time_waited = time_waited + STACK_ACTION_RETRY_WAIT - if time_waited >= wait_timeout: - LOG.error("Stack %(action)s not completed within " - "%(wait)s seconds", - {'action': action, - 'wait': wait_timeout, - 'stack': stack_id}) - # Some times, a second delete request succeeds in cleaning - # up the stack when the first request is stuck forever in - # Pending state - if action == 'delete': - try: - heatclient.delete(stack_id) - except Exception: - pass - return - else: - LOG.error( - "Stack %(stack_name)s %(action)s not " - "completed within %(time)s seconds where " - "stack owner is %(stack_owner)s", - {'stack_name': stack.stack_name, - 'action': action, - 'time': wait_timeout, - 'stack_owner': stack.stack_owner}) - return None - - def is_config_complete(self, stack_id, tenant_id, - network_function_details): - success_status = "COMPLETED" - failure_status = "ERROR" - intermediate_status = "IN_PROGRESS" - _, resource_owner_tenant_id = ( - self._get_resource_owner_context()) - heatclient = self._get_heat_client(tenant_id) - if not heatclient: - return failure_status - with nfp_ctx_mgr.HeatContextManager as hcm: - stack = hcm.retry(heatclient.get, stack_id) - if stack.stack_status == 'DELETE_FAILED': - return failure_status - elif stack.stack_status == 'CREATE_COMPLETE': - self.loadbalancer_post_stack_create(network_function_details) - return success_status - elif stack.stack_status == 'UPDATE_COMPLETE': - return success_status - elif stack.stack_status == 'DELETE_COMPLETE': - LOG.info("Stack %(stack)s is deleted", - {'stack': stack_id}) - return failure_status - elif stack.stack_status == 'CREATE_FAILED': - return failure_status - elif stack.stack_status == 'UPDATE_FAILED': - return failure_status - elif stack.stack_status not in [ - 'UPDATE_IN_PROGRESS', 'CREATE_IN_PROGRESS', - 'DELETE_IN_PROGRESS']: - return intermediate_status - - def check_config_complete(self, nfp_context): - success_status = "COMPLETED" - failure_status = "ERROR" - intermediate_status = "IN_PROGRESS" - - provider_tenant_id = nfp_context['tenant_id'] - stack_id = nfp_context['config_policy_id'] - - heatclient = self._get_heat_client(provider_tenant_id) - if not heatclient: - return failure_status - with nfp_ctx_mgr.HeatContextManager as hcm: - stack = hcm.retry(heatclient.get, stack_id) - if stack.stack_status == 'DELETE_FAILED': - return failure_status - elif stack.stack_status == 'CREATE_COMPLETE': - self._post_stack_create(nfp_context) - return success_status - elif stack.stack_status == 'UPDATE_COMPLETE': - return success_status - elif stack.stack_status == 'DELETE_COMPLETE': - LOG.info("Stack %(stack)s is deleted", - {'stack': stack_id}) - return failure_status - elif stack.stack_status == 'CREATE_FAILED': - return failure_status - elif stack.stack_status == 'UPDATE_FAILED': - return failure_status - elif stack.stack_status not in [ - 'UPDATE_IN_PROGRESS', 'CREATE_IN_PROGRESS', - 'DELETE_IN_PROGRESS']: - return intermediate_status - - def is_config_delete_complete(self, stack_id, tenant_id, - network_function=None): - success_status = "COMPLETED" - failure_status = "ERROR" - intermediate_status = "IN_PROGRESS" - heatclient = self._get_heat_client(tenant_id) - if not heatclient: - return failure_status - with nfp_ctx_mgr.HeatContextManager as hcm: - stack = hcm.retry(heatclient.get, stack_id) - if stack.stack_status == 'DELETE_FAILED': - return failure_status - elif stack.stack_status == 'CREATE_COMPLETE': - return failure_status - elif stack.stack_status == 'DELETE_COMPLETE': - LOG.info("Stack %(stack)s is deleted", - {'stack': stack_id}) - if network_function: - self._post_stack_cleanup(network_function) - return success_status - elif stack.stack_status == 'CREATE_FAILED': - return failure_status - elif stack.stack_status == 'UPDATE_FAILED': - return failure_status - elif stack.stack_status not in [ - 'UPDATE_IN_PROGRESS', 'CREATE_IN_PROGRESS', - 'DELETE_IN_PROGRESS']: - return intermediate_status - - def get_service_details_from_nfp_context(self, nfp_context): - network_function = nfp_context['network_function'] - service_details = nfp_context['service_details'] - mgmt_ip = '' - if nfp_context.get('network_function_device'): - mgmt_ip = nfp_context['network_function_device']['mgmt_ip_address'] - config_policy_id = network_function['config_policy_id'] - servicechain_instance = nfp_context['service_chain_instance'] - servicechain_node = nfp_context['service_chain_node'] - - consumer_policy_target_group = nfp_context['consumer']['ptg'] - provider_policy_target_group = nfp_context['provider']['ptg'] - provider_port = nfp_context['provider']['port'] - provider_subnet = nfp_context['provider']['subnet'] - consumer_port = nfp_context['consumer']['port'] - consumer_subnet = nfp_context['consumer']['subnet'] - service_details['consuming_external_policies'] = nfp_context[ - 'consuming_eps_details'] - service_details['consuming_ptgs_details'] = nfp_context[ - 'consuming_ptgs_details'] - - return { - 'service_profile': None, - 'service_details': service_details, - 'servicechain_node': servicechain_node, - 'servicechain_instance': servicechain_instance, - 'consumer_port': consumer_port, - 'consumer_subnet': consumer_subnet, - 'provider_port': provider_port, - 'provider_subnet': provider_subnet, - 'mgmt_ip': mgmt_ip, - 'config_policy_id': config_policy_id, - 'provider_ptg': provider_policy_target_group, - 'consumer_ptg': consumer_policy_target_group, - 'consuming_external_policies': - service_details['consuming_external_policies'], - 'consuming_ptgs_details': - service_details['consuming_ptgs_details'] - } - - def apply_config(self, network_function_details): - service_details = self.get_service_details(network_function_details) - service_profile = service_details['service_profile'] - service_chain_node = service_details['servicechain_node'] - service_chain_instance = service_details['servicechain_instance'] - provider = service_details['provider_ptg'] - consumer = service_details['consumer_ptg'] - consumer_port = service_details['consumer_port'] - provider_port = service_details['provider_port'] - mgmt_ip = service_details['mgmt_ip'] - - service_details = transport.parse_service_flavor_string( - service_profile['service_flavor']) - - auth_token, resource_owner_tenant_id = ( - self._get_resource_owner_context()) - provider_tenant_id = provider['tenant_id'] - heatclient = self._get_heat_client(provider_tenant_id) - if not heatclient: - return None - stack_name = ("stack_" + service_chain_instance['name'] + - service_chain_node['name'] + - service_chain_instance['id'][:8] + - service_chain_node['id'][:8] + '-' + - time.strftime("%Y%m%d%H%M%S")) - # Heat does not accept space in stack name - stack_name = stack_name.replace(" ", "") - stack_template, stack_params = self._update_node_config( - auth_token, provider_tenant_id, service_profile, - service_chain_node, service_chain_instance, provider, - consumer_port, network_function_details['network_function'], - provider_port, mgmt_ip=mgmt_ip, consumer=consumer) - - if not stack_template and not stack_params: - return None - - with nfp_ctx_mgr.HeatContextManager as hcm: - stack = hcm.retry(heatclient.create, stack_name, - stack_template, stack_params) - stack_id = stack['stack']['id'] - LOG.info("Created stack with ID %(stack_id)s and " - "name %(stack_name)s for provider PTG %(provider)s", - {'stack_id': stack_id, 'stack_name': stack_name, - 'provider': provider['id']}) - - return stack_id - - def apply_heat_config(self, nfp_context): - service_details = self.get_service_details_from_nfp_context( - nfp_context) - - network_function = nfp_context['network_function'] - service_chain_node = service_details['servicechain_node'] - service_chain_instance = service_details['servicechain_instance'] - provider = service_details['provider_ptg'] - consumer = service_details['consumer_ptg'] - consumer_port = service_details['consumer_port'] - provider_port = service_details['provider_port'] - mgmt_ip = service_details['mgmt_ip'] - - auth_token = nfp_context['resource_owner_context']['admin_token'] - provider_tenant_id = nfp_context['tenant_id'] - heatclient = self._get_heat_client(provider_tenant_id, - assign_admin=True) - if not heatclient: - return None - - stack_template, stack_params = self._create_node_config_data( - auth_token, provider_tenant_id, - service_chain_node, service_chain_instance, - provider, provider_port, consumer, consumer_port, - network_function, mgmt_ip, service_details) - - if not stack_template and not stack_params: - return None - - if not heatclient: - return None - - stack_name = ("stack_" + service_chain_instance['name'] + - service_chain_node['name'] + - service_chain_instance['id'][:8] + - service_chain_node['id'][:8] + '-' + - time.strftime("%Y%m%d%H%M%S")) - # Heat does not accept space in stack name - stack_name = stack_name.replace(" ", "") - - with nfp_ctx_mgr.HeatContextManager as hcm: - stack = hcm.retry(heatclient.create, stack_name, - stack_template, stack_params) - - stack_id = stack['stack']['id'] - LOG.info("Created stack with ID %(stack_id)s and " - "name %(stack_name)s for provider PTG %(provider)s", - {'stack_id': stack_id, 'stack_name': stack_name, - 'provider': provider['id']}) - - return stack_id - - def delete_config(self, stack_id, tenant_id, network_function=None): - - try: - heatclient = self._get_heat_client(tenant_id) - if not heatclient: - return None - if network_function: - self._pre_stack_cleanup(network_function) - with nfp_ctx_mgr.HeatContextManager as hcm: - hcm.retry(heatclient.delete, stack_id) - except Exception as err: - # Log the error and continue with VM delete in case of *aas - # cleanup failure - LOG.exception("Cleaning up the service chain stack failed " - "with Error: %(error)s", {'error': err}) - return None - - return stack_id - - def is_update_config_supported(self, service_type): - return ( - False - if (service_type == pconst.FIREWALL) - else True - ) - - def _update(self, auth_token, resource_owner_tenant_id, service_profile, - service_chain_node, service_chain_instance, provider, - consumer_port, network_function, provider_port, stack_id, - consumer=None, mgmt_ip=None, pt_added_or_removed=False): - # If it is not a Node config update or PT change for LB, no op - service_details = transport.parse_service_flavor_string( - service_profile['service_flavor']) - base_mode_support = (True if service_details['device_type'] == 'None' - else False) - provider_tenant_id = provider['tenant_id'] - heatclient = self._get_heat_client(provider_tenant_id) - if not heatclient: - return None - - if not base_mode_support and not mgmt_ip: - LOG.error("Service information is not available with Service " - "Orchestrator on node update") - return None - - stack_template, stack_params = self._update_node_config( - auth_token, provider_tenant_id, service_profile, - service_chain_node, service_chain_instance, provider, - consumer_port, network_function, provider_port, - update=True, mgmt_ip=mgmt_ip, consumer=consumer) - if not stack_template and not stack_params: - return None - - if stack_id: - with nfp_ctx_mgr.HeatContextManager as hcm: - hcm.retry(heatclient.update, stack_id, - stack_template, stack_params) - if not stack_id: - stack_name = ("stack_" + service_chain_instance['name'] + - service_chain_node['name'] + - service_chain_instance['id'][:8] + - service_chain_node['id'][:8] + '-' + - time.strftime("%Y%m%d%H%M%S")) - with nfp_ctx_mgr.HeatContextManager as hcm: - stack = hcm.retry(heatclient.create, stack_name, - stack_template, stack_params) - - stack_id = stack["stack"]["id"] - return stack_id - - def update_config(self, network_function_details, stack_id): - service_details = self.get_service_details(network_function_details) - service_profile = service_details['service_profile'] - service_chain_node = service_details['servicechain_node'] - service_chain_instance = service_details['servicechain_instance'] - provider = service_details['provider_ptg'] - consumer = service_details['consumer_ptg'] - consumer_port = service_details['consumer_port'] - provider_port = service_details['provider_port'] - mgmt_ip = service_details['mgmt_ip'] - - auth_token, resource_owner_tenant_id = ( - self._get_resource_owner_context()) - stack_id = self._update(auth_token, resource_owner_tenant_id, - service_profile, service_chain_node, - service_chain_instance, provider, - consumer_port, network_function_details[ - 'network_function'], - provider_port, - stack_id, consumer=consumer, mgmt_ip=mgmt_ip) - - if not stack_id: - return None - return stack_id - - def handle_policy_target_operations(self, network_function_details, - policy_target, operation): - service_details = self.get_service_details(network_function_details) - service_profile = service_details['service_profile'] - service_chain_node = service_details['servicechain_node'] - service_chain_instance = service_details['servicechain_instance'] - provider = service_details['provider_ptg'] - consumer_port = service_details['consumer_port'] - provider_port = service_details['provider_port'] - mgmt_ip = service_details['mgmt_ip'] - stack_id = service_details['config_policy_id'] - - if service_profile['service_type'] == pconst.LOADBALANCERV2: - if self._is_service_target(policy_target): - return - auth_token, resource_owner_tenant_id = ( - self._get_resource_owner_context()) - try: - stack_id = self._update(auth_token, resource_owner_tenant_id, - service_profile, service_chain_node, - service_chain_instance, provider, - consumer_port, - network_function_details[ - 'network_function'], - provider_port, stack_id, - mgmt_ip=mgmt_ip, - pt_added_or_removed=True) - return stack_id - except Exception: - LOG.exception("Processing policy target %(operation)s " - " failed", {'operation': operation}) - return None - - def notify_chain_parameters_updated(self, network_function_details): - pass # We are not using the classifier specified in redirect Rule - - def handle_consumer_ptg_operations(self, network_function_details, - policy_target_group, operation): - service_details = self.get_service_details(network_function_details) - service_profile = service_details['service_profile'] - service_chain_node = service_details['servicechain_node'] - service_chain_instance = service_details['servicechain_instance'] - provider = service_details['provider_ptg'] - consumer_port = service_details['consumer_port'] - provider_port = service_details['provider_port'] - mgmt_ip = service_details['mgmt_ip'] - stack_id = service_details['config_policy_id'] - - if service_profile['service_type'] == pconst.FIREWALL: - auth_token, resource_owner_tenant_id = ( - self._get_resource_owner_context()) - try: - stack_id = self._update(auth_token, resource_owner_tenant_id, - service_profile, service_chain_node, - service_chain_instance, provider, - consumer_port, - network_function_details[ - 'network_function'], - provider_port, stack_id, - mgmt_ip=mgmt_ip) - - if not stack_id: - return None - return stack_id - except Exception: - LOG.exception( - "Processing policy target group " - "%(operation)s failed", {'operation': operation}) - return None diff --git a/gbpservice/nfp/orchestrator/context.py b/gbpservice/nfp/orchestrator/context.py deleted file mode 100644 index 688e839d4..000000000 --- a/gbpservice/nfp/orchestrator/context.py +++ /dev/null @@ -1,520 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - - -class Subnet(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - return { - 'cidr': self.data.get('cidr'), - 'id': self.data.get('id'), - 'gateway_ip': self.data.get('gateway_ip'), - 'name': self.data.get('name') - } - return self.data - - -class Port(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - return { - 'id': self.data.get('id'), - 'ip_address': self.data.get('ip_address'), - 'mac_address': self.data.get('mac_address'), - 'mac': self.data.get('mac'), - 'name': self.data.get('name'), - 'fixed_ips': self.data.get('fixed_ips'), - 'gateway_ip': self.data.get('gateway_ip'), - 'neutron_port': self.data.get('neutron_port'), - 'cidr': self.data.get('cidr') - } - return self.data - - -class Pt(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - return { - 'id': self.data.get('id'), - 'name': self.data.get('name'), - 'port_id': self.data.get('port_id'), - 'policy_target_group_id': self.data.get( - 'policy_target_group_id'), - 'group_default_gateway': self.data.get( - 'group_default_gateway'), - 'proxy_gateway': self.data.get( - 'proxy_gateway') - } - return self.data - - -class Ptg(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - return { - 'id': self.data.get('id'), - 'name': self.data.get('name'), - 'provided_policy_rule_sets': self.data.get( - 'provided_policy_rule_sets'), - 'proxied_group_id': self.data.get( - 'proxied_group_id'), - 'policy_targets': self.data.get('policy_targets'), - 'tenant_id': self.data.get('tenant_id'), - 'subnets': self.data.get('subnets'), - 'l2_policy_id': self.data.get('l2_policy_id') - } - return self.data - - -class NetworkFunctionDevice(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - return { - 'id': self.data.get('id'), - 'interfaces_in_use': self.data.get('interfaces_in_use'), - 'status': self.data.get('status'), - 'mgmt_ip_address': self.data.get('mgmt_ip_address'), - 'monitoring_port_id': self.data.get('monitoring_port_id'), - 'reference_count': self.data.get('reference_count'), - 'mgmt_port_id': self.data.get('mgmt_port_id'), - 'tenant_id': self.data.get('tenant_id'), - } - return self.data - - -class NetworkFunctionInstance(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - return { - 'id': self.data.get('id'), - 'status': self.data.get('status'), - 'port_info': self.data.get('port_info'), - 'network_function_device_id': self.data.get( - 'network_function_device_id'), - 'tenant_id': self.data.get('tenant_id'), - 'name': self.data.get('name') - } - return self.data - - -class NetworkFunction(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - return { - 'name': self.data.get('name'), - 'status': self.data.get('status'), - 'service_id': self.data.get('service_id'), - 'config_policy_id': self.data.get('config_policy_id'), - 'service_profile_id': self.data.get('service_profile_id'), - 'service_chain_id': self.data.get('service_chain_id'), - 'id': self.data.get('id'), - 'tenant_id': self.data.get('tenant_id'), - 'network_function_instances': self.data.get( - 'network_function_instances'), - 'description': self.data.get('description') - } - return self.data - - -class ResourceOwnerContext(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - return { - 'admin_token': self.data.get('admin_token'), - 'admin_tenant_id': self.data.get('admin_tenant_id'), - 'tenant_name': self.data.get('tenant_name'), - 'tenant': self.data.get('tenant') - } - return self.data - - -class Management(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - return { - 'port': self.data.get('port') - } - return self.data - - -class Provider(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - context = { - 'subnet': Subnet( - self.data.get('subnet')).purge(), - 'port_model': self.data.get('port_model'), - 'port_classification': self.data.get('port_classification') - } - - if type(self.data.get('pt')) is list: - pt_list = [] - for pt in self.data['pt']: - pt_list.append(Pt(pt).purge()) - context['pt'] = pt_list - else: - context['pt'] = Pt(self.data.get('pt')).purge() - - if type(self.data.get('ptg')) is list: - ptg_list = [] - for ptg in self.data['ptg']: - ptg_list.append(Ptg(ptg).purge()) - context['ptg'] = ptg_list - else: - context['ptg'] = Ptg(self.data.get('ptg')).purge() - - if type(self.data.get('port')) is list: - port_list = [] - for port in self.data['port']: - port_list.append(Port(port).purge()) - context['port'] = port_list - else: - context['port'] = Port(self.data.get('port')).purge() - - return context - return self.data - - -class Consumer(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - context = { - 'subnet': Subnet( - self.data.get('subnet')).purge(), - 'port_model': self.data.get('port_model'), - 'port_classification': self.data.get('port_classification') - } - if type(self.data.get('pt')) is list: - pt_list = [] - for pt in self.data['pt']: - pt_list.append(Pt(pt).purge()) - context['pt'] = pt_list - else: - context['pt'] = Pt(self.data.get('pt')).purge() - - if type(self.data.get('ptg')) is list: - ptg_list = [] - for ptg in self.data['ptg']: - ptg_list.append(Ptg(ptg).purge()) - context['ptg'] = ptg_list - else: - context['ptg'] = Ptg(self.data.get('ptg')).purge() - - if type(self.data.get('port')) is list: - port_list = [] - for port in self.data['port']: - port_list.append(Port(port).purge()) - context['port'] = port_list - else: - context['port'] = Port(self.data.get('port')).purge() - - return context - return self.data - - -class ScNodes(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - sc_service_profile = self.data.get('sc_service_profile') - context = {'sc_service_profile': {}} - if sc_service_profile: - context['sc_service_profile'][ - 'service_type'] = sc_service_profile.get('service_type') - return context - return self.data - - -class ServiceChainSpecs(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - sc_nodes = self.data.get('sc_nodes') - if type(sc_nodes) is list: - context = [] - for sc_node in sc_nodes: - context.append(ScNodes(sc_node).purge()) - return { - 'sc_nodes': context - } - else: - return { - 'sc_nodes': ScNodes(sc_nodes).purge() - } - return self.data - - -class ServiceChainInstance(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - return { - 'id': self.data.get('id'), - 'config_param_values': self.data.get('config_param_values'), - 'name': self.data.get('name'), - 'provider_ptg_id': self.data.get('provider_ptg_id') - } - return self.data - - -class ConsumingPtgsDetails(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - context = {} - context['ptg'] = Ptg(self.data.get('ptg')).purge() - subnets = self.data.get('subnets') - if type(subnets) is list: - subnet_ctxt = [] - for subnet in subnets: - subnet_ctxt.append(Subnet(subnet).purge()) - context['subnets'] = subnet_ctxt - else: - context['subnets'] = Subnet(subnets).purge() - return context - return self.data - - -class ServiceChainNode(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - return { - 'service_profile_id': self.data.get('service_profile_id'), - 'service_type': self.data.get('service_type'), - 'config': self.data.get('config'), - 'name': self.data.get('name'), - 'id': self.data.get('id') - } - return self.data - - -class ServiceDetails(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - return { - 'service_vendor': self.data.get('service_vendor'), - 'service_type': self.data.get('service_type'), - 'network_mode': self.data.get('network_mode'), - 'image_name': self.data.get('image_name'), - 'device_type': self.data.get('device_type'), - } - return self.data - - -class ConsumingEpsDetails(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - return { - 'id': self.data.get('id') - } - return self.data - - -class ServerGrpId(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - return { - 'result': self.data.get('result') - } - return self.data - - -class ServiceProfile(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - return { - 'id': self.data.get('id'), - 'service_flavor': self.data.get('service_flavor'), - 'service_type': self.data.get('service_type') - } - return self.data - - -class LogContext(object): - - def __init__(self, data): - self.data = data - - def purge(self): - if self.data: - return { - 'meta_id': self.data.get('meta_id', '-'), - 'nfi_id': self.data.get('nfi_id', '-'), - 'nfd_id': self.data.get('nfd_id', '-'), - 'path': self.data.get('path'), - 'auth_token': self.data.get('auth_token'), - 'namespace': self.data.get('namespace') - } - return self.data - - -class NfpContext(object): - - def __init__(self, data): - self.data = data - - def purge(self): - - context = { - 'active_nfd_ids': self.data.get('active_nfd_ids'), - 'device_without_plugging': self.data.get( - 'device_without_plugging'), - 'id': self.data.get('id'), # event id - 'key': self.data.get('key'), # event key - 'admin_token': self.data.get('admin_token'), - 'event_desc': self.data.get('event_desc'), - 'config_policy_id': self.data.get('config_policy_id'), - 'management_ptg_id': self.data.get('management_ptg_id'), - 'network_function_mode': self.data.get('network_function_mode'), - 'files': self.data.get('files'), - 'base_mode_support': self.data.get('base_mode_support'), - 'share_existing_device': self.data.get('share_existing_device'), - 'tenant_id': self.data.get('tenant_id'), - 'binding_key': self.data.get('binding_key'), - 'provider_metadata': self.data.get('provider_metadata'), - 'admin_tenant_id': self.data.get('admin_tenant_id'), - 'is_nfi_in_graph': self.data.get('is_nfi_in_graph'), - 'network_function_device': NetworkFunctionDevice( - self.data.get('network_function_device')).purge(), - 'network_function_instance': NetworkFunctionInstance( - self.data.get('network_function_instance')).purge(), - 'network_function': NetworkFunction( - self.data.get('network_function')).purge(), - 'resource_owner_context': ResourceOwnerContext( - self.data.get('resource_owner_context')).purge(), - 'management': Management( - self.data.get('management')).purge(), - 'provider': Provider( - self.data.get('provider')).purge(), - 'consumer': Consumer( - self.data.get('consumer')).purge(), - 'service_chain_instance': ServiceChainInstance( - self.data.get('service_chain_instance')).purge(), - 'service_details': ServiceDetails( - self.data.get('service_details')).purge(), - 'service_chain_node': ServiceChainNode( - self.data.get('service_chain_node')).purge(), - 'server_grp_id': ServerGrpId( - self.data.get('server_grp_id')).purge(), - 'service_profile': ServiceProfile( - self.data.get('service_profile')).purge(), - 'log_context': LogContext(self.data.get('log_context')).purge(), - 'enable_port_security': self.data.get('enable_port_security') - } - - service_chain_specs = self.data.get('service_chain_specs') - if type(service_chain_specs) is list: - ctxt = [] - for sc_specs in service_chain_specs: - ctxt.append(ServiceChainSpecs(sc_specs).purge()) - context['service_chain_specs'] = ctxt - else: - context['service_chain_specs'] = ServiceChainSpecs( - service_chain_specs).purge() - - consuming_ptgs_details = self.data.get('consuming_ptgs_details') - if type(consuming_ptgs_details) is list: - ctxt = [] - for ptgs_details in consuming_ptgs_details: - ctxt.append(ConsumingPtgsDetails(ptgs_details).purge()) - context['consuming_ptgs_details'] = ctxt - else: - context['consuming_ptgs_details'] = ConsumingPtgsDetails( - consuming_ptgs_details).purge() - - consuming_eps_details = self.data.get('consuming_eps_details') - if type(consuming_eps_details) is list: - ctxt = [] - for eps_details in consuming_eps_details: - ctxt.append(ConsumingEpsDetails(eps_details).purge()) - context['consuming_eps_details'] = ctxt - else: - context['consuming_eps_details'] = ConsumingEpsDetails( - consuming_eps_details).purge() - - return context diff --git a/gbpservice/nfp/orchestrator/db/__init__.py b/gbpservice/nfp/orchestrator/db/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/nfp/orchestrator/db/common_db_mixin.py b/gbpservice/nfp/orchestrator/db/common_db_mixin.py deleted file mode 100644 index f8d00b438..000000000 --- a/gbpservice/nfp/orchestrator/db/common_db_mixin.py +++ /dev/null @@ -1,183 +0,0 @@ -# Copyright (c) 2014 OpenStack Foundation. -# All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import weakref - -from neutron_lib.db import utils as db_utils -from oslo_db.sqlalchemy import utils as sa_utils -import six - - -# TODO(ashu): Below class need to extend neutron's CommonDbMixin. -class CommonDbMixin(object): - """Common methods used in core and service plugins.""" - # Plugins, mixin classes implementing extension will register - # hooks into the dict below for "augmenting" the "core way" of - # building a query for retrieving objects from a model class. - # To this aim, the register_model_query_hook and unregister_query_hook - # from this class should be invoked - _model_query_hooks = {} - - # This dictionary will store methods for extending attributes of - # api resources. Mixins can use this dict for adding their own methods - _dict_extend_functions = {} - - @classmethod - def register_model_query_hook(cls, model, name, query_hook, filter_hook, - result_filters=None): - """Register a hook to be invoked when a query is executed. - - Add the hooks to the _model_query_hooks dict. Models are the keys - of this dict, whereas the value is another dict mapping hook names to - callables performing the hook. - Each hook has a "query" component, used to build the query expression - and a "filter" component, which is used to build the filter expression. - - Query hooks take as input the query being built and return a - transformed query expression. - - Filter hooks take as input the filter expression being built and return - a transformed filter expression - """ - model_hooks = cls._model_query_hooks.get(model) - if not model_hooks: - # add key to dict - model_hooks = {} - cls._model_query_hooks[model] = model_hooks - model_hooks[name] = {'query': query_hook, 'filter': filter_hook, - 'result_filters': result_filters} - - @property - def safe_reference(self): - """Return a weakref to the instance. - - Minimize the potential for the instance persisting - unnecessarily in memory by returning a weakref proxy that - won't prevent deallocation. - """ - return weakref.proxy(self) - - def _model_query(self, session, model, is_admin=False): - query = session.query(model) - # define basic filter condition for model query - query_filter = None - # Execute query hooks registered from mixins and plugins - query_hooks = self._model_query_hooks.get(model, {}) - for _name, hooks in six.iteritems(query_hooks): - query_hook = hooks.get('query') - if isinstance(query_hook, six.string_types): - query_hook = getattr(self, query_hook, None) - if query_hook: - query = query_hook(model, query) - - filter_hook = hooks.get('filter') - if isinstance(filter_hook, six.string_types): - filter_hook = getattr(self, filter_hook, None) - if filter_hook: - query_filter = filter_hook(model, query_filter) - - if query_filter is not None: - query = query.filter(query_filter) - return query - - def _fields(self, resource, fields): - if fields: - return dict(((key, item) for key, item in list(resource.items()) - if key in fields)) - return resource - - def _get_tenant_id_for_create(self, resource): - return resource['tenant_id'] - - def _get_by_id(self, session, model, id): - query = self._model_query(session, model) - return query.filter(model.id == id).one() - - def _apply_filters_to_query(self, query, model, filters): - if filters: - for key, value in six.iteritems(filters): - column = getattr(model, key, None) - if column: - query = query.filter(column.in_(value)) - - query_hooks = self._model_query_hooks.get(model, {}) - for _name, hooks in six.iteritems(query_hooks): - result_filter = hooks.get('result_filters', None) - if isinstance(result_filter, six.string_types): - result_filter = getattr(self, result_filter, None) - - if result_filter: - query = result_filter(query, filters) - return query - - def _apply_dict_extend_functions(self, resource_type, - response, db_object): - for func in self._dict_extend_functions.get( - resource_type, []): - args = (response, db_object) - if isinstance(func, six.string_types): - func = getattr(self, func, None) - else: - # must call unbound method - use self as 1st argument - args = (self,) + args - if func: - func(*args) - - def _get_collection_query(self, session, model, filters=None, - sorts=None, limit=None, marker_obj=None, - page_reverse=False): - collection = self._model_query(session, model) - collection = self._apply_filters_to_query(collection, model, filters) - if sorts: - sort_keys = db_utils.get_and_validate_sort_keys(sorts, model) - sort_dirs = db_utils.get_sort_dirs(sorts, page_reverse) - collection = sa_utils.paginate_query( - collection, model, limit, - marker=marker_obj, - sort_keys=sort_keys, - sort_dirs=sort_dirs) - - return collection - - def _get_collection(self, session, model, dict_func, filters=None, - fields=None, sorts=None, limit=None, marker_obj=None, - page_reverse=False): - query = self._get_collection_query(session, model, filters=filters, - sorts=sorts, - limit=limit, - marker_obj=marker_obj, - page_reverse=page_reverse) - items = [dict_func(c, fields) for c in query] - if limit and page_reverse: - items.reverse() - return items - - def _get_collection_count(self, model, filters=None): - return self._get_collection_query(model, filters).count() - - def _get_marker_obj(self, resource, limit, marker): - if limit and marker: - return getattr(self, '_get_%s' % resource)(marker) - return None - - def _filter_non_model_columns(self, data, model): - """Filter non model columns - - Remove all the attributes from data which are not columns of - the model passed as second parameter. - """ - columns = [c.name for c in model.__table__.columns] - return dict((k, v) for (k, v) in - six.iteritems(data) if k in columns) diff --git a/gbpservice/nfp/orchestrator/db/nfp_db.py b/gbpservice/nfp/orchestrator/db/nfp_db.py deleted file mode 100644 index dc763eaca..000000000 --- a/gbpservice/nfp/orchestrator/db/nfp_db.py +++ /dev/null @@ -1,777 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or -# implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from oslo_serialization import jsonutils -from oslo_utils import uuidutils -from sqlalchemy.orm import exc - -from gbpservice.nfp.common import exceptions as nfp_exc -from gbpservice.nfp.orchestrator.db import nfp_db_model - -from gbpservice.nfp.core import log as nfp_logging - -LOG = nfp_logging.getLogger(__name__) - - -class NFPDbBase(object): - - def __init__(self, *args, **kwargs): - super(NFPDbBase, self).__init__(*args, **kwargs) - - def create_network_function(self, session, network_function): - with session.begin(subtransactions=True): - network_function_db = nfp_db_model.NetworkFunction( - id=uuidutils.generate_uuid(), - name=network_function['name'], - description=network_function.get('description'), - project_id=network_function['tenant_id'], - service_id=network_function['service_id'], - service_chain_id=network_function.get('service_chain_id'), - service_profile_id=network_function['service_profile_id'], - service_config=network_function.get('service_config'), - config_policy_id=network_function.get('config_policy_id'), - status=network_function['status']) - session.add(network_function_db) - return self._make_network_function_dict(network_function_db) - - def _get_network_function(self, session, network_function_id): - try: - return self._get_by_id( - session, nfp_db_model.NetworkFunction, network_function_id) - except exc.NoResultFound: - raise nfp_exc.NetworkFunctionNotFound( - network_function_id=network_function_id) - - def update_network_function(self, session, network_function_id, - updated_network_function): - with session.begin(subtransactions=True): - network_function_db = self._get_network_function( - session, network_function_id) - network_function_db.update(updated_network_function) - if 'status' in list(updated_network_function.keys()): - updated_network_function_map = { - 'status': updated_network_function['status'] - } - if updated_network_function.get('status_description'): - updated_network_function_map.update( - {'status_details': updated_network_function[ - 'status_description']}) - self.update_node_instance_network_function_map(session, - network_function_db['service_id'], - network_function_db['service_chain_id'], - updated_network_function_map) - return self._make_network_function_dict(network_function_db) - - def delete_network_function(self, session, network_function_id): - with session.begin(subtransactions=True): - network_function_db = self._get_network_function( - session, network_function_id) - session.delete(network_function_db) - # deleting sc-node-instance-nf entry - self.delete_node_instance_network_function_map( - session, network_function_id) - - def get_network_function(self, session, network_function_id, fields=None): - service = self._get_network_function(session, network_function_id) - return self._make_network_function_dict(service, fields) - - def get_network_functions(self, session, filters=None, fields=None, - sorts=None, limit=None, marker=None, - page_reverse=False): - marker_obj = self._get_marker_obj( - 'network_functions', limit, marker) - return self._get_collection(session, nfp_db_model.NetworkFunction, - self._make_network_function_dict, - filters=filters, fields=fields, - sorts=sorts, limit=limit, - marker_obj=marker_obj, - page_reverse=page_reverse) - - def _set_port_info_for_nfi(self, session, network_function_instance_db, - network_function_instance, is_update=False): - nfi_db = network_function_instance_db - port_info = network_function_instance.get('port_info') - with session.begin(subtransactions=True): - nfi_db.port_info = [] - for port in port_info: - port_info_db = nfp_db_model.PortInfo( - id=port['id'], - port_model=port['port_model'], - port_classification=port.get('port_classification'), - port_role=port.get('port_role')) - if is_update: - session.merge(port_info_db) - else: - session.add(port_info_db) - session.flush() # Any alternatives for flush ?? - assoc = nfp_db_model.NSIPortAssociation( - network_function_instance_id=( - network_function_instance_db['id']), - data_port_id=port['id']) - nfi_db.port_info.append(assoc) - del network_function_instance['port_info'] - - def create_network_function_instance(self, session, - network_function_instance): - with session.begin(subtransactions=True): - network_function_instance_db = ( - nfp_db_model.NetworkFunctionInstance( - id=uuidutils.generate_uuid(), - name=network_function_instance['name'], - project_id=network_function_instance['tenant_id'], - description=network_function_instance.get('description'), - network_function_id=network_function_instance[ - 'network_function_id'], - network_function_device_id=network_function_instance.get( - 'network_function_device_id'), - ha_state=network_function_instance.get('ha_state'), - status=network_function_instance['status'])) - session.add(network_function_instance_db) - self._set_port_info_for_nfi(session, network_function_instance_db, - network_function_instance) - return self._make_network_function_instance_dict( - network_function_instance_db) - - def _get_network_function_instance(self, session, - network_function_instance_id): - try: - return self._get_by_id( - session, - nfp_db_model.NetworkFunctionInstance, - network_function_instance_id) - except exc.NoResultFound: - raise nfp_exc.NetworkFunctionInstanceNotFound( - network_function_instance_id=network_function_instance_id) - - def update_network_function_instance(self, session, - network_function_instance_id, - updated_network_function_instance): - with session.begin(subtransactions=True): - network_function_instance_db = self._get_network_function_instance( - session, network_function_instance_id) - if 'port_info' in updated_network_function_instance: - self._set_port_info_for_nfi( - session, - network_function_instance_db, - updated_network_function_instance, is_update=True) - network_function_instance_db.update( - updated_network_function_instance) - return self._make_network_function_instance_dict( - network_function_instance_db) - - def delete_network_function_instance(self, session, - network_function_instance_id): - with session.begin(subtransactions=True): - network_function_instance_db = self._get_network_function_instance( - session, network_function_instance_id) - for port in network_function_instance_db.port_info: - self.delete_port_info(session, port['data_port_id']) - session.delete(network_function_instance_db) - - def get_network_function_instance(self, session, - network_function_instance_id, - fields=None): - network_function_instance = self._get_network_function_instance( - session, network_function_instance_id) - return self._make_network_function_instance_dict( - network_function_instance, fields) - - def get_network_function_instances(self, session, filters=None, - fields=None, sorts=None, limit=None, - marker=None, page_reverse=False): - port_info = None - marker_obj = self._get_marker_obj( - 'network_function_instances', limit, marker) - if filters: - port_info = filters.pop('port_info', None) - nfis = self._get_collection( - session, nfp_db_model.NetworkFunctionInstance, - self._make_network_function_instance_dict, - filters=filters, fields=fields, sorts=sorts, limit=limit, - marker_obj=marker_obj, page_reverse=page_reverse) - filtered_nfis = [] - if port_info: - for nfi in nfis: - if port_info == nfi['port_info']: - filtered_nfis.append(nfi) - return filtered_nfis - return nfis - - def _set_mgmt_port_for_nfd(self, session, network_function_device_db, - network_function_device, is_update=False): - nfd_db = network_function_device_db - mgmt_port_id = network_function_device.get('mgmt_port_id') - if not mgmt_port_id: - nfd_db.mgmt_port_id = None - return - with session.begin(subtransactions=True): - port_info_db = nfp_db_model.PortInfo( - id=mgmt_port_id['id'], - port_model=mgmt_port_id['port_model'], - port_classification=mgmt_port_id['port_classification'], - port_role=mgmt_port_id['port_role']) - if is_update: - session.merge(port_info_db) - else: - session.add(port_info_db) - session.flush() - nfd_db.mgmt_port_id = port_info_db['id'] - - def _set_monitoring_port_id_for_nfd(self, session, - network_function_device_db, - network_function_device, - is_update=False): - nfd_db = network_function_device_db - monitoring_port_id = network_function_device.get( - 'monitoring_port_id') - if not monitoring_port_id: - nfd_db.monitoring_port_id = None - return - with session.begin(subtransactions=True): - port_info_db = nfp_db_model.PortInfo( - id=monitoring_port_id['id'], - port_model=monitoring_port_id['port_model'], - port_classification=monitoring_port_id[ - 'port_classification'], - port_role=monitoring_port_id['port_role']) - if is_update: - session.merge(port_info_db) - else: - session.add(port_info_db) - session.flush() - nfd_db.monitoring_port_id = monitoring_port_id['id'] - - def _set_monitoring_port_network_for_nfd(self, session, - network_function_device_db, - network_function_device, - is_update=False): - nfd_db = network_function_device_db - monitoring_port_network = network_function_device.get( - 'monitoring_port_network') - if not monitoring_port_network: - nfd_db.monitoring_port_network = None - return - with session.begin(subtransactions=True): - network_info_db = nfp_db_model.NetworkInfo( - id=monitoring_port_network['id'], - network_model=monitoring_port_network['network_model']) - session.add(network_info_db) - session.flush() - nfd_db.monitoring_port_network = ( - monitoring_port_network['id']) - del network_function_device['monitoring_port_network'] - - def _set_provider_metadata_for_nfd(self, session, - network_function_device_db, - network_function_device, - is_update=False): - nfd_db = network_function_device_db - provider_metadata = nfd_db['provider_metadata'] - - if is_update: - if provider_metadata: - provider_metadata = jsonutils.loads(provider_metadata) - updated_provider_metadata_str = network_function_device.pop( - 'provider_metadata', {}) - if not updated_provider_metadata_str: - return - if updated_provider_metadata_str: - updated_provider_metadata = jsonutils.loads( - updated_provider_metadata_str) - if (type(updated_provider_metadata) is dict and - updated_provider_metadata and provider_metadata): - updated_provider_metadata.update(provider_metadata) - provider_metadata_str = jsonutils.dumps(updated_provider_metadata) - else: - if not provider_metadata: - provider_metadata_str = '' - return - provider_metadata_str = jsonutils.dumps(provider_metadata) - nfd_db.provider_metadata = provider_metadata_str - - def create_network_function_device(self, session, network_function_device): - with session.begin(subtransactions=True): - network_function_device_db = nfp_db_model.NetworkFunctionDevice( - id=(network_function_device.get('id') or - uuidutils.generate_uuid()), - name=network_function_device['name'], - description=network_function_device.get('description'), - project_id=network_function_device['tenant_id'], - mgmt_ip_address=network_function_device[ - 'mgmt_ip_address'], - service_vendor=network_function_device.get('service_vendor'), - max_interfaces=network_function_device['max_interfaces'], - reference_count=network_function_device['reference_count'], - interfaces_in_use=network_function_device['interfaces_in_use'], - status=network_function_device['status'], - gateway_port=network_function_device.get('gateway_port')) - session.add(network_function_device_db) - self._set_mgmt_port_for_nfd( - session, network_function_device_db, network_function_device) - self._set_monitoring_port_id_for_nfd( - session, network_function_device_db, network_function_device) - self._set_monitoring_port_network_for_nfd( - session, network_function_device_db, network_function_device) - self._set_provider_metadata_for_nfd( - session, network_function_device_db, network_function_device) - return self._make_network_function_device_dict( - network_function_device_db) - - def _get_network_function_device(self, session, - network_function_device_id): - try: - nfd = self._get_by_id( - session, - nfp_db_model.NetworkFunctionDevice, - network_function_device_id) - return nfd - except exc.NoResultFound: - raise nfp_exc.NetworkFunctionDeviceNotFound( - network_function_device_id=network_function_device_id) - - def update_network_function_device(self, session, - network_function_device_id, - updated_network_function_device): - with session.begin(subtransactions=True): - network_function_device_db = self._get_network_function_device( - session, network_function_device_id) - if updated_network_function_device.get('provider_metadata'): - updated_network_function_device[ - 'provider_metadata'] = jsonutils.dumps( - updated_network_function_device[ - 'provider_metadata']) - if updated_network_function_device.get('mgmt_port_id'): - self._set_mgmt_port_for_nfd( - session, - network_function_device_db, - updated_network_function_device, - is_update=True) - - if 'monitoring_port_id' in updated_network_function_device: - self._set_monitoring_port_id_for_nfd( - session, - network_function_device_db, - updated_network_function_device, - is_update=True) - if 'monitoring_port_network' in updated_network_function_device: - self._set_monitoring_port_network_for_nfd( - session, - network_function_device_db, - updated_network_function_device, - is_update=True) - self._set_provider_metadata_for_nfd( - session, network_function_device_db, - updated_network_function_device, - is_update=True) - mgmt_port_id = ( - updated_network_function_device.pop('mgmt_port_id', None)) - if mgmt_port_id: - updated_network_function_device[ - 'mgmt_port_id'] = mgmt_port_id['id'] - - monitoring_port_id = ( - updated_network_function_device.pop('monitoring_port_id', - None)) - if monitoring_port_id: - updated_network_function_device[ - 'monitoring_port_id'] = monitoring_port_id['id'] - network_function_device_db.update(updated_network_function_device) - updated_network_function_device['mgmt_port_id'] = mgmt_port_id - updated_network_function_device[ - 'monitoring_port_id'] = monitoring_port_id - - return self._make_network_function_device_dict( - network_function_device_db) - - def delete_network_function_device(self, session, - network_function_device_id): - with session.begin(subtransactions=True): - network_function_device_db = self._get_network_function_device( - session, network_function_device_id) - if network_function_device_db.mgmt_port_id: - self.delete_port_info(session, - network_function_device_db.mgmt_port_id) - if network_function_device_db.monitoring_port_id: - self.delete_port_info( - session, - network_function_device_db.monitoring_port_id) - if network_function_device_db.monitoring_port_network: - self.delete_network_info( - session, - network_function_device_db.monitoring_port_network) - session.delete(network_function_device_db) - - def get_network_function_device(self, session, network_function_device_id, - fields=None): - network_function_device = self._get_network_function_device( - session, network_function_device_id) - return self._make_network_function_device_dict( - network_function_device, fields) - - def get_network_function_devices(self, session, filters=None, fields=None, - sorts=None, limit=None, marker=None, - page_reverse=False): - marker_obj = self._get_marker_obj( - 'network_function_devices', limit, marker) - return self._get_collection(session, - nfp_db_model.NetworkFunctionDevice, - self._make_network_function_device_dict, - filters=filters, fields=fields, - sorts=sorts, limit=limit, - marker_obj=marker_obj, - page_reverse=page_reverse) - - def increment_network_function_device_count(self, session, - network_function_device_id, - field_name, - updated_value=1): - with session.begin(subtransactions=True): - network_function_device = self._get_network_function_device( - session, network_function_device_id) - value = network_function_device[field_name] - value += updated_value - update_device = ( - {field_name: value}) - self.update_network_function_device(session, - network_function_device_id, - update_device) - - def decrement_network_function_device_count(self, session, - network_function_device_id, - field_name, - updated_value=1): - with session.begin(subtransactions=True): - network_function_device = self._get_network_function_device( - session, network_function_device_id) - value = network_function_device[field_name] - value -= updated_value - update_device = ( - {field_name: value}) - self.update_network_function_device(session, - network_function_device_id, - update_device) - - def get_port_info(self, session, port_id, fields=None): - port_info = self._get_port_info(session, port_id) - return self._make_port_info_dict(port_info, fields) - - def _get_port_info(self, session, port_id): - try: - return self._get_by_id( - session, nfp_db_model.PortInfo, port_id) - except exc.NoResultFound: - raise nfp_exc.NFPPortNotFound(port_id=port_id) - - def delete_port_info(self, session, port_id): - with session.begin(subtransactions=True): - port_info_db = self._get_port_info(session, port_id) - session.delete(port_info_db) - - def delete_network_info(self, session, network_id): - with session.begin(subtransactions=True): - network_info_db = self._get_network_info(session, network_id) - session.delete(network_info_db) - - def get_network_info(self, session, network_id, fields=None): - network_info = self._get_network_info(session, network_id) - return self._make_network_info_dict(network_info, fields) - - def _get_network_info(self, session, network_id): - return self._get_by_id( - session, nfp_db_model.NetworkInfo, network_id) - - def _make_port_info_dict(self, port_info, fields): - res = { - 'id': port_info['id'], - 'port_classification': port_info['port_classification'], - 'port_model': port_info['port_model'], - 'port_role': port_info['port_role'] - } - return res - - def _make_network_info_dict(self, network_info, fields): - res = { - 'id': network_info['id'], - 'network_model': network_info['network_model'], - } - return res - - def _make_network_function_dict(self, network_function, fields=None): - res = {'id': network_function['id'], - 'tenant_id': network_function['project_id'], - 'name': network_function['name'], - 'description': network_function['description'], - 'service_id': network_function['service_id'], - 'service_chain_id': network_function['service_chain_id'], - 'service_profile_id': network_function['service_profile_id'], - 'service_config': network_function['service_config'], - 'config_policy_id': network_function['config_policy_id'], - 'status': network_function['status'] - } - res['network_function_instances'] = [ - nfi['id'] for nfi in network_function[ - 'network_function_instances']] - return res - - def _make_network_function_instance_dict(self, nfi, fields=None): - res = {'id': nfi['id'], - 'tenant_id': nfi['project_id'], - 'name': nfi['name'], - 'description': nfi['description'], - 'ha_state': nfi['ha_state'], - 'network_function_id': nfi['network_function_id'], - 'network_function_device_id': nfi['network_function_device_id'], - 'status': nfi['status'] - } - res['port_info'] = [ - port['data_port_id'] for port in nfi['port_info']] - return res - - def _make_network_function_device_dict(self, nfd, fields=None): - res = {'id': nfd['id'], - 'tenant_id': nfd['project_id'], - 'name': nfd['name'], - 'description': nfd['description'], - 'mgmt_ip_address': nfd['mgmt_ip_address'], - 'mgmt_port_id': nfd['mgmt_port_id'], - 'monitoring_port_id': nfd['monitoring_port_id'], - 'monitoring_port_network': nfd['monitoring_port_network'], - 'service_vendor': nfd['service_vendor'], - 'max_interfaces': nfd['max_interfaces'], - 'reference_count': nfd['reference_count'], - 'interfaces_in_use': nfd['interfaces_in_use'], - 'status': nfd['status'], - 'gateway_port': nfd.get('gateway_port') - } - if nfd.get('provider_metadata'): - res.update({'provider_metadata': nfd['provider_metadata']}) - return res - - def add_cluster_info(self, session, cluster_info): - with session.begin(subtransactions=True): - cluster_info = nfp_db_model.ClusterInfo( - id=cluster_info['id'], project_id=cluster_info['tenant_id'], - network_function_device_id=cluster_info[ - 'network_function_device_id'], - cluster_group=cluster_info[ - 'cluster_group'], virtual_ip=cluster_info['virtual_ip'], - multicast_ip=cluster_info.get('multicast_ip', None), - cluster_name=cluster_info.get('cluster_name', None) - ) - session.add(cluster_info) - setattr(cluster_info, 'tenant_id', cluster_info['project_id']) - return cluster_info - - def insert_cluster_records(self, session, cluster_infos): - with session.begin(subtransactions=True): - for cluster_info in cluster_infos: - cluster_info = nfp_db_model.ClusterInfo( - id=cluster_info['id'], - project_id=cluster_info['tenant_id'], - network_function_device_id=cluster_info[ - 'network_function_device_id'], - cluster_group=cluster_info['cluster_group'], - virtual_ip=cluster_info['virtual_ip'], - multicast_ip=cluster_info.get('multicast_ip', None), - cluster_name=cluster_info.get('cluster_name', None)) - session.add(cluster_info) - - def get_cluster_info(self, session, _id): - try: - return self._get_by_id( - session, - nfp_db_model.ClusterInfo, _id) - except exc.NoResultFound: - raise nfp_exc.ClusterInfoNotFound(id=_id) - - def get_all_cluster_info(self, session, filters=None, fields=None, - sorts=None, limit=None, marker=None, - page_reverse=False): - marker_obj = self._get_marker_obj( - 'nfd_cluster_mapping_info', limit, marker) - return self._get_collection(session, - nfp_db_model.ClusterInfo, - self._get_cluster_info_dict, - filters=filters, fields=fields, - sorts=sorts, limit=limit, - marker_obj=marker_obj, - page_reverse=page_reverse) - - def del_cluster_info(self, session, _id): - with session.begin(subtransactions=True): - cluster_info = self.get_cluster_info(session, _id) - session.delete(cluster_info) - - def delete_cluster_info(self, session, port_id_list): - for port_id in port_id_list: - self.del_cluster_info(session, port_id) - - def _get_cluster_info_dict(self, cluster_info, filters=None, fields=None, - sorts=None, limit=None, marker=None, - page_reverse=False): - return { - 'id': cluster_info['id'], 'tenant_id': cluster_info['project_id'], - 'network_function_device_id': cluster_info[ - 'network_function_device_id'], - 'cluster_group': cluster_info['cluster_group'], - 'multicast_ip': cluster_info['multicast_ip'], - 'cluster_name': cluster_info['cluster_name'] - } - - def add_service_gateway_details(self, session, service_gw_details): - primary_gw_vip_pt, secondary_gw_vip_pt = self._get_vip_pt_ids( - service_gw_details.get('gateway_vips')) - if isinstance(service_gw_details['primary_instance_gw_pt'], dict): - primary_instance_gw_pt = service_gw_details[ - 'primary_instance_gw_pt']['id'] - secondary_instance_gw_pt = service_gw_details.get( - 'secondary_instance_gw_pt', {}).get('id') - else: - primary_instance_gw_pt = service_gw_details[ - 'primary_instance_gw_pt'] - secondary_instance_gw_pt = service_gw_details.get( - 'secondary_instance_gw_pt') - with session.begin(subtransactions=True): - gw_detail = nfp_db_model.ServiceGatewayDetails( - id=service_gw_details['id'], - network_function_id=service_gw_details[ - 'network_function_id'], - gateway_ptg=service_gw_details['gw_ptg'], - primary_instance_gw_pt=primary_instance_gw_pt, - secondary_instance_gw_pt=secondary_instance_gw_pt, - primary_gw_vip_pt=primary_gw_vip_pt, - secondary_gw_vip_pt=secondary_gw_vip_pt - ) - session.add(gw_detail) - return gw_detail - - def _get_vip_pt_ids(self, vips): - if not vips: - return None, None - else: - if isinstance(vips, list): - primary_gw_vip_pt = vips[0]['id'] - secondary_gw_vip_pt = vips[1]['id'] if len(vips) == 2 else None - return primary_gw_vip_pt, secondary_gw_vip_pt - elif isinstance(vips, dict): - return vips['primary_gw_vip_pt'], vips['secondary_gw_vip_pt'] - else: - return None, None - - def get_providers_for_gateway(self, session, _id): - svc_gw = nfp_db_model.ServiceGatewayDetails - try: - with session.begin(subtransactions=True): - return self._get_gw_info_dict(session.query(svc_gw).filter( - svc_gw.gateway_ptg == _id).all()) - except exc.NoResultFound: - raise - - def get_gateway_detail(self, session, nf_id): - svc_gw = nfp_db_model.ServiceGatewayDetails - try: - with session.begin(subtransactions=True): - return self._get_gw_info_dict(session.query(svc_gw).filter( - svc_gw.network_function_id == nf_id).one()) - except exc.NoResultFound: - LOG.warning("Gateway detail doesn't exist for Network Function" - " %s ", nf_id) - raise - - def _get_gw_info_dict(self, gw): - if not gw: - return - if isinstance(gw, list): - return [dict(id=info['id'], - network_function_id=info['network_function_id'], - gateway_ptg=info['gateway_ptg'], - primary_instance_gw_pt=info['primary_instance_gw_pt'], - secondary_instance_gw_pt=info[ - 'secondary_instance_gw_pt'], - primary_gw_vip_pt=info['primary_gw_vip_pt'], - secondary_gw_vip_pt=info['secondary_gw_vip_pt'] - ) for info in gw] - - return {'id': gw['id'], - 'network_function_id': gw['network_function_id'], - 'gateway_ptg': gw['gateway_ptg'], - 'primary_instance_gw_pt': gw['primary_instance_gw_pt'], - 'secondary_instance_gw_pt': gw['secondary_instance_gw_pt'], - 'primary_gw_vip_pt': gw['primary_gw_vip_pt'], - 'secondary_gw_vip_pt': gw['secondary_gw_vip_pt']} - - def create_node_instance_network_function_map(self, session, - sc_node_id, sc_instance_id, - network_function_id, - status, status_details): - with session.begin(subtransactions=True): - sc_node_instance_ns_map = ( - nfp_db_model.ServiceNodeInstanceNetworkFunctionMapping( - sc_node_id=sc_node_id, - sc_instance_id=sc_instance_id, - network_function_id=network_function_id, - status=status, - status_details=status_details)) - session.add(sc_node_instance_ns_map) - - def update_node_instance_network_function_map( - self, session, sc_node_id, sc_instance_id, - updated_node_instance_network_function_map): - with session.begin(subtransactions=True): - node_instance_network_function_map = ( - self.get_node_instance_network_function_map(session, - sc_node_id, sc_instance_id)) - node_instance_network_function_map.update( - updated_node_instance_network_function_map) - return node_instance_network_function_map - - def get_node_instance_network_function_map(self, session, sc_node_id=None, - sc_instance_id=None): - try: - with session.begin(subtransactions=True): - query = session.query( - nfp_db_model.ServiceNodeInstanceNetworkFunctionMapping) - if sc_node_id: - query = query.filter_by(sc_node_id=sc_node_id) - if sc_instance_id: - query = query.filter_by(sc_instance_id=sc_instance_id) - return query.first() - except exc.NoResultFound: - return None - - def get_node_instance_network_function_maps(self, session, sc_instance_id): - try: - with session.begin(subtransactions=True): - query = session.query( - nfp_db_model.ServiceNodeInstanceNetworkFunctionMapping) - query = query.filter_by(sc_instance_id=sc_instance_id) - from neutron.db import sqlalchemyutils - collection = sqlalchemyutils.paginate_query( - query, - nfp_db_model.ServiceNodeInstanceNetworkFunctionMapping, - None, None) - return [item for item in collection] - except exc.NoResultFound: - return [] - - def delete_node_instance_network_function_map(self, session, - network_function_id): - try: - with session.begin(subtransactions=True): - sc_node_instance_ns_maps = (session.query( - nfp_db_model.ServiceNodeInstanceNetworkFunctionMapping). - filter_by(network_function_id=network_function_id).all()) - for sc_node_instance_ns_map in sc_node_instance_ns_maps: - session.delete(sc_node_instance_ns_map) - except exc.NoResultFound: - return None diff --git a/gbpservice/nfp/orchestrator/db/nfp_db_model.py b/gbpservice/nfp/orchestrator/db/nfp_db_model.py deleted file mode 100644 index a4974a7d0..000000000 --- a/gbpservice/nfp/orchestrator/db/nfp_db_model.py +++ /dev/null @@ -1,183 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or -# implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from neutron_lib.db import model_base -import sqlalchemy as sa -from sqlalchemy.ext.declarative import declarative_base -from sqlalchemy import orm - -from gbpservice.nfp.common import constants as nfp_constants - -TENANT_ID_MAX_LEN = 255 -DESCRIPTION_MAX_LEN = 4096 - - -class HasStatus(object): - """status mixin, add to subclasses that have a status.""" - - status = sa.Column(sa.String(16), nullable=False, index=True) - - -class HasStatusDescription(HasStatus): - """Status with description mixin.""" - - status_description = sa.Column(sa.String(DESCRIPTION_MAX_LEN)) - - -BASE = declarative_base(cls=model_base.NeutronBaseV2) - - -class PortInfo(BASE, model_base.HasId, model_base.HasProject): - """Represents the Port Information""" - __tablename__ = 'nfp_port_infos' - - port_model = sa.Column(sa.Enum(nfp_constants.NEUTRON_PORT, - nfp_constants.GBP_PORT, - name='port_model')) - port_classification = sa.Column(sa.Enum(nfp_constants.PROVIDER, - nfp_constants.CONSUMER, - nfp_constants.MANAGEMENT, - nfp_constants.MONITOR, - name='port_classification')) - port_role = sa.Column(sa.Enum(nfp_constants.ACTIVE_PORT, - nfp_constants.STANDBY_PORT, - nfp_constants.MASTER_PORT, - name='port_role'), - nullable=True) - - -class NetworkInfo(BASE, model_base.HasId, model_base.HasProject): - """Represents the Network Service Instance""" - __tablename__ = 'nfp_network_infos' - - network_model = sa.Column(sa.Enum(nfp_constants.NEUTRON_NETWORK, - nfp_constants.GBP_NETWORK, - name='network_model'), - nullable=False) - - -class NSIPortAssociation(BASE): - """One to many relation between NSIs and DataPorts.""" - __tablename__ = 'nfp_nfi_dataport_associations' - - network_function_instance_id = sa.Column( - sa.String(36), - sa.ForeignKey('nfp_network_function_instances.id'), primary_key=True) - data_port_id = sa.Column(sa.String(36), - sa.ForeignKey('nfp_port_infos.id', - ondelete='CASCADE'), - primary_key=True) - - -class NetworkFunctionInstance(BASE, model_base.HasId, model_base.HasProject, - HasStatusDescription): - """Represents the Network Function Instance""" - __tablename__ = 'nfp_network_function_instances' - - name = sa.Column(sa.String(255)) - description = sa.Column(sa.String(255)) - ha_state = sa.Column(sa.String(255)) - network_function_id = sa.Column( - sa.String(36), - sa.ForeignKey('nfp_network_functions.id', ondelete="SET NULL"), - nullable=True) - network_function_device_id = sa.Column( - sa.String(36), - sa.ForeignKey('nfp_network_function_devices.id', ondelete="SET NULL"), - nullable=True) - port_info = orm.relationship( - NSIPortAssociation, - cascade='all, delete-orphan') - - -class NetworkFunction(BASE, model_base.HasId, model_base.HasProject, - HasStatusDescription): - """Represents the Network Function object""" - __tablename__ = 'nfp_network_functions' - - name = sa.Column(sa.String(255)) - description = sa.Column(sa.String(1024)) - service_id = sa.Column(sa.String(36), nullable=False) - service_chain_id = sa.Column(sa.String(36), nullable=True) - service_profile_id = sa.Column(sa.String(36), nullable=False) - service_config = sa.Column(sa.TEXT) - config_policy_id = sa.Column(sa.String(36), nullable=True) - network_function_instances = orm.relationship( - NetworkFunctionInstance, - backref='network_function') - - -class NetworkFunctionDevice(BASE, model_base.HasId, model_base.HasProject, - HasStatusDescription): - """Represents the Network Function Device""" - __tablename__ = 'nfp_network_function_devices' - - name = sa.Column(sa.String(255)) - description = sa.Column(sa.String(255)) - mgmt_ip_address = sa.Column(sa.String(36), nullable=True) - mgmt_port_id = sa.Column(sa.String(36), - sa.ForeignKey('nfp_port_infos.id', - ondelete='SET NULL'), - nullable=True) - monitoring_port_id = sa.Column(sa.String(36), - sa.ForeignKey('nfp_port_infos.id', - ondelete='SET NULL'), - nullable=True) - monitoring_port_network = sa.Column(sa.String(36), - sa.ForeignKey('nfp_network_infos.id', - ondelete='SET NULL'), - nullable=True) - service_vendor = sa.Column(sa.String(36), nullable=False, index=True) - max_interfaces = sa.Column(sa.Integer(), nullable=False) - reference_count = sa.Column(sa.Integer(), nullable=False) - interfaces_in_use = sa.Column(sa.Integer(), nullable=False) - provider_metadata = sa.Column(sa.String(1024), nullable=True) - gateway_port = sa.Column(sa.String(36), nullable=True) - - -class ClusterInfo(BASE, model_base.HasId, model_base.HasProject): - """ - This table contains info about the ports participating in - cluster and optional. - """ - __tablename__ = 'nfd_cluster_mapping_info' - network_function_device_id = sa.Column(sa.String(36), nullable=False) - cluster_group = sa.Column(sa.Integer(), nullable=True) - virtual_ip = sa.Column(sa.String(36), nullable=True) - multicast_ip = sa.Column(sa.String(36), nullable=True) - cluster_name = sa.Column(sa.String(36), nullable=True) - - -class ServiceGatewayDetails(BASE, model_base.HasId): - __tablename__ = 'nfp_service_gateway_info' - network_function_id = sa.Column(sa.String(36), sa.ForeignKey( - 'nfp_network_functions.id', ondelete='CASCADE'), nullable=False, - primary_key=True) - gateway_ptg = sa.Column(sa.String(36), nullable=False) - primary_instance_gw_pt = sa.Column(sa.String(36), nullable=True) - secondary_instance_gw_pt = sa.Column(sa.String(36), nullable=True) - primary_gw_vip_pt = sa.Column(sa.String(36), nullable=True) - secondary_gw_vip_pt = sa.Column(sa.String(36), nullable=True) - - -class ServiceNodeInstanceNetworkFunctionMapping(BASE, model_base.BASEV2): - """ServiceChainInstance to NFP network function mapping.""" - - __tablename__ = 'ncp_node_instance_network_function_mappings' - sc_instance_id = sa.Column(sa.String(36), - nullable=False, primary_key=True) - sc_node_id = sa.Column(sa.String(36), - nullable=False, primary_key=True) - network_function_id = sa.Column(sa.String(36), nullable=True) - status = sa.Column(sa.String(50), nullable=True) - status_details = sa.Column(sa.String(4096), nullable=True) diff --git a/gbpservice/nfp/orchestrator/drivers/orchestration_driver.py b/gbpservice/nfp/orchestrator/drivers/orchestration_driver.py deleted file mode 100644 index 1a2e86373..000000000 --- a/gbpservice/nfp/orchestrator/drivers/orchestration_driver.py +++ /dev/null @@ -1,1070 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import ast -from collections import defaultdict - -from gbpservice.nfp.common import constants as nfp_constants -from gbpservice.nfp.common import data_formatter as df -from gbpservice.nfp.common import exceptions -from gbpservice.nfp.core import executor as nfp_executor -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.lib import nfp_context_manager as nfp_ctx_mgr -from gbpservice.nfp.orchestrator.coal.networking import ( - nfp_gbp_network_driver -) -from gbpservice.nfp.orchestrator.coal.networking import ( - nfp_neutron_network_driver -) -from gbpservice.nfp.orchestrator.openstack import openstack_driver - -LOG = nfp_logging.getLogger(__name__) - - -def _set_network_handler(f): - def wrapped(self, *args, **kwargs): - if type(args[0]) == dict: - device_data = args[0] - else: - device_data = args[1] - if device_data.get('service_details'): - network_mode = device_data['service_details'].get('network_mode') - if network_mode: - kwargs['network_handler'] = self.network_handlers[network_mode] - return f(self, *args, **kwargs) - return wrapped - - -class OrchestrationDriver(object): - """Generic Driver class for orchestration of virtual appliances - - Launches the VM with all the management and data ports and a new VM - is launched for each Network Service Instance - """ - - def __init__(self, config, supports_device_sharing=False, - supports_hotplug=False, max_interfaces=8): - self.service_vendor = 'general' - self.supports_device_sharing = supports_device_sharing - self.supports_hotplug = supports_hotplug - self.maximum_interfaces = max_interfaces - self.identity_handler = openstack_driver.KeystoneClient(config) - self.compute_handler_nova = openstack_driver.NovaClient(config) - self.network_handlers = { - nfp_constants.GBP_MODE: - nfp_gbp_network_driver.NFPGBPNetworkDriver(config), - nfp_constants.NEUTRON_MODE: - nfp_neutron_network_driver.NFPNeutronNetworkDriver(config) - } - self.config = config - - def _get_admin_tenant_id(self, token=None): - with nfp_ctx_mgr.KeystoneContextManager as kcm: - if not token: - token = kcm.retry( - self.identity_handler.get_admin_token, tries=3) - admin_tenant_name = ( - self.config.nfp_keystone_authtoken.admin_tenant_name) - admin_tenant_id = kcm.retry(self.identity_handler.get_tenant_id, - token, - admin_tenant_name, tries=3) - return admin_tenant_id - - def _get_token(self, device_data_token): - - with nfp_ctx_mgr.KeystoneContextManager as kcm: - token = (device_data_token - if device_data_token - else kcm.retry( - self.identity_handler.get_admin_token, tries=3)) - return token - - def _is_device_sharing_supported(self): - return False - - def _create_management_interface(self, token, admin_tenant_id, - device_data, network_handler): - - name = nfp_constants.MANAGEMENT_INTERFACE_NAME - mgmt_interface = network_handler.create_port( - token, - admin_tenant_id, - device_data['management_network_info']['id'], - name=name) - - return {'id': mgmt_interface['id'], - 'port_id': mgmt_interface['port_id'], - 'port_model': (nfp_constants.GBP_PORT - if device_data['service_details'][ - 'network_mode'] == - nfp_constants.GBP_MODE - else nfp_constants.NEUTRON_PORT), - 'port_classification': nfp_constants.MANAGEMENT, - 'port_role': None} - - def _delete_interface(self, device_data, interface, - network_handler=None): - token = self._get_token(device_data.get('token')) - if not token: - return None - - network_handler.delete_port(token, interface['id']) - - def _get_interfaces_for_device_create(self, token, admin_tenant_id, - network_handler, device_data): - try: - mgmt_interface = self._create_management_interface( - token, - admin_tenant_id, - device_data, - network_handler) - device_data['interfaces'] = [mgmt_interface] - except Exception as e: - LOG.exception('Failed to get interfaces for device creation.' - 'Error: %(error)s', {'error': e}) - - def _delete_interfaces(self, device_data, interfaces, - network_handler=None): - for interface in interfaces: - self._delete_interface( - device_data, interface, - network_handler=network_handler) - - def _verify_provider_metadata(self, image_name, metadata): - provider_metadata = {} - try: - for attr in metadata: - if attr in nfp_constants.METADATA_SUPPORTED_ATTRIBUTES: - provider_metadata[attr] = ast.literal_eval(metadata[attr]) - except Exception as e: - LOG.error('Wrong metadata: %(metadata)s provided for ' - 'image name: %(image_name)s. Error: %(error)s', - {'image_name': image_name, 'metadata': metadata, - 'error': e}) - return None - return provider_metadata - - def _get_provider_metadata(self, device_data, image_name): - token = self._get_token(device_data.get('token')) - if not token: - return None - with nfp_ctx_mgr.NovaContextManager as ncm: - metadata = ncm.retry(self.compute_handler_nova.get_image_metadata, - token, - self._get_admin_tenant_id(token=token), - image_name) - provider_metadata = self._verify_provider_metadata( - image_name, metadata) - if not provider_metadata: - return {} - return provider_metadata - - def _get_provider_metadata_fast(self, token, - admin_tenant_id, image_name, device_data): - with nfp_ctx_mgr.NovaContextManager as ncm: - metadata = ncm.retry(self.compute_handler_nova.get_image_metadata, - token, - admin_tenant_id, - image_name) - provider_metadata = self._verify_provider_metadata( - image_name, metadata) - if not provider_metadata: - return {} - return provider_metadata - - def _update_self_with_provider_metadata(self, provider_metadata, attr): - attr_value = getattr(self, attr) - if attr in provider_metadata: - setattr(self, attr, provider_metadata[attr]) - else: - LOG.debug("Provider metadata specified in image, doesn't contains " - "%s value, proceeding with default value " - "%s", (attr, attr_value)) - - def _update_provider_metadata(self, device_data, token=None): - provider_metadata = {} - try: - image_name = self._get_image_name(device_data) - provider_metadata = self._get_provider_metadata(device_data, - image_name) - LOG.debug("Provider metadata, specified in image: %s", - provider_metadata) - if provider_metadata: - self._update_self_with_provider_metadata( - provider_metadata, - nfp_constants.MAXIMUM_INTERFACES) - self._update_self_with_provider_metadata( - provider_metadata, - nfp_constants.SUPPORTS_HOTPLUG) - else: - LOG.debug("No provider metadata specified in image," - " proceeding with default values") - except Exception: - LOG.error("Error while getting metadata for image name:" - "%(image_name)s, proceeding with default values", - {'image_name': image_name}) - return provider_metadata - - def _update_provider_metadata_fast(self, token, admin_tenant_id, - image_name, device_data): - provider_metadata = None - try: - provider_metadata = self._get_provider_metadata_fast( - token, admin_tenant_id, image_name, device_data) - LOG.debug("Provider metadata, specified in image: %s", - provider_metadata) - if provider_metadata: - self._update_self_with_provider_metadata( - provider_metadata, - nfp_constants.MAXIMUM_INTERFACES) - self._update_self_with_provider_metadata( - provider_metadata, - nfp_constants.SUPPORTS_HOTPLUG) - else: - LOG.debug("No provider metadata specified in image," - " proceeding with default values") - except Exception: - LOG.error("Error while getting metadata for image name: " - "%(image_name)s, proceeding with default values", - {'image_name': image_name}) - return provider_metadata - - def _get_image_name(self, device_data): - if device_data['service_details'].get('image_name'): - image_name = device_data['service_details']['image_name'] - else: - LOG.debug("No image name provided in service profile's " - "service flavor field, image will be selected " - "based on service vendor's name : %s", - (device_data['service_details']['service_vendor'])) - image_name = device_data['service_details']['service_vendor'] - image_name = '%s' % image_name.lower() - device_data['service_details']['image_name'] = image_name - return image_name - - def _get_service_type(self, token, service_profile_id, network_handler): - service_profile = network_handler.get_service_profile( - token, service_profile_id) - return service_profile['service_type'] - - def _get_device_service_types_map(self, token, devices, network_handler): - device_service_types_map = defaultdict(set) - for device in devices: - for network_function in device['network_functions']: - service_type = self._get_service_type( - token, - network_function['service_profile_id'], - network_handler) - device_service_types_map[device['id']].add(service_type) - return device_service_types_map - - def get_image_id(self, nova, token, admin_tenant_id, image_name): - try: - image_id = nova.get_image_id(token, admin_tenant_id, image_name) - return image_id - except Exception as e: - LOG.error('Failed to get image id for device creation.' - ' image name: %(image_name)s. Error: %(error)s', - {'image_name': image_name, 'error': e}) - - def create_instance(self, nova, token, admin_tenant_id, - image_id, flavor, interfaces_to_attach, - instance_name, volume_support, - volume_size, files=None, user_data=None, - server_grp_id=None): - try: - instance_id = nova.create_instance( - token, admin_tenant_id, image_id, flavor, - interfaces_to_attach, instance_name, volume_support, - volume_size, files=files, userdata=user_data, - server_grp_id=server_grp_id) - return instance_id - except Exception as e: - LOG.error('Failed to create instance.' - 'Error: %(error)s', {'error': e}) - - def get_neutron_port_details(self, network_handler, token, port_id): - try: - (mgmt_ip_address, - mgmt_mac, mgmt_cidr, gateway_ip, - mgmt_port, mgmt_subnet) = ( - network_handler.get_neutron_port_details(token, port_id)) - - result = {'neutron_port': mgmt_port['port'], - 'neutron_subnet': mgmt_subnet['subnet'], - 'ip_address': mgmt_ip_address, - 'mac': mgmt_mac, - 'cidr': mgmt_cidr, - 'gateway_ip': gateway_ip} - return result - except Exception as e: - import sys - import traceback - exc_type, exc_value, exc_traceback = sys.exc_info() - LOG.error(traceback.format_exception(exc_type, exc_value, - exc_traceback)) - LOG.error('Failed to get management port details. ' - 'Error: %(error)s', {'error': e}) - - @_set_network_handler - def create_network_function_device(self, device_data, - network_handler=None): - """ Create a NFD - - :param device_data: NFD data - :type device_data: dict - - :returns: None -- when there is a failure in creating NFD - :return: dict -- NFD created - - :raises: exceptions.IncompleteData, - exceptions.ComputePolicyNotSupported - """ - self._validate_create_nfd_data(device_data) - - token = device_data['token'] - admin_tenant_id = device_data['admin_tenant_id'] - image_name = self._get_image_name(device_data) - - pre_launch_executor = nfp_executor.TaskExecutor(jobs=3) - - image_id_result = {} - provider_metadata_result = {} - - pre_launch_executor.add_job('UPDATE_PROVIDER_METADATA', - self._update_provider_metadata_fast, - token, admin_tenant_id, - image_name, device_data, - result_store=provider_metadata_result) - pre_launch_executor.add_job('GET_INTERFACES_FOR_DEVICE_CREATE', - self._get_interfaces_for_device_create, - token, admin_tenant_id, - network_handler, device_data) - pre_launch_executor.add_job('GET_IMAGE_ID', - self.get_image_id, - self.compute_handler_nova, token, - admin_tenant_id, - image_name, result_store=image_id_result) - - pre_launch_executor.fire() - - interfaces, image_id, provider_metadata = ( - self._validate_pre_launch_executor_results( - network_handler, - device_data, - image_name, - image_id_result, - provider_metadata_result)) - if not interfaces: - return None - - management_interface = interfaces[0] - flavor = self._get_service_instance_flavor(device_data) - - interfaces_to_attach = [] - try: - for interface in interfaces: - interfaces_to_attach.append({'port': interface['port_id']}) - if provider_metadata.get('supports_hotplug') is False: - self._update_interfaces_for_non_hotplug_support( - network_handler, - interfaces, - interfaces_to_attach, - device_data) - except Exception as e: - LOG.error('Failed to fetch list of interfaces to attach' - ' for device creation %(error)s', {'error': e}) - self._delete_interfaces(device_data, interfaces, - network_handler=network_handler) - return None - - instance_name = device_data['name'] - - create_instance_executor = nfp_executor.TaskExecutor(jobs=3) - instance_id_result = {} - port_details_result = {} - volume_support = device_data['volume_support'] - volume_size = device_data['volume_size'] - create_instance_executor.add_job( - 'CREATE_INSTANCE', self.create_instance, - self.compute_handler_nova, token, - admin_tenant_id, image_id, flavor, - interfaces_to_attach, instance_name, - volume_support, volume_size, - files=device_data.get('files'), - user_data=device_data.get('user_data'), - result_store=instance_id_result) - - create_instance_executor.add_job( - 'GET_NEUTRON_PORT_DETAILS', - self.get_neutron_port_details, - network_handler, token, - management_interface['port_id'], - result_store=port_details_result) - - create_instance_executor.fire() - - instance_id, mgmt_neutron_port_info = ( - self._validate_create_instance_executor_results( - network_handler, - device_data, - interfaces, - instance_id_result, - port_details_result)) - if not instance_id: - return None - - mgmt_ip_address = mgmt_neutron_port_info['ip_address'] - return {'id': instance_id, - 'name': instance_name, - 'provider_metadata': provider_metadata, - 'mgmt_ip_address': mgmt_ip_address, - 'mgmt_port_id': interfaces[0], - 'mgmt_neutron_port_info': mgmt_neutron_port_info, - 'max_interfaces': self.maximum_interfaces, - 'interfaces_in_use': len(interfaces_to_attach), - 'description': ''} # TODO(RPM): what should be the description - - def _validate_create_nfd_data(self, device_data): - if ( - any(key not in device_data - for key in ['service_details', - 'name', - 'management_network_info', - 'ports']) or - - type(device_data['service_details']) is not dict or - - any(key not in device_data['service_details'] - for key in ['service_vendor', - 'device_type', - 'network_mode']) or - - any(key not in device_data['management_network_info'] - for key in ['id']) or - - type(device_data['ports']) is not list or - - any(key not in port - for port in device_data['ports'] - for key in ['id', - 'port_classification', - 'port_model']) - ): - raise exceptions.IncompleteData() - - if ( - device_data['service_details']['device_type'] != - nfp_constants.NOVA_MODE - ): - raise exceptions.ComputePolicyNotSupported( - compute_policy=device_data['service_details']['device_type']) - - def _validate_pre_launch_executor_results(self, network_handler, - device_data, - image_name, - image_id_result, - provider_metadata_result, - server_grp_id_result=None): - interfaces = device_data.pop('interfaces', None) - if not interfaces: - LOG.exception('Failed to get interfaces for device creation.') - return None, None, None - - image_id = image_id_result.get('result', None) - if not image_id: - LOG.error('Failed to get image id for device creation.') - self._delete_interfaces(device_data, interfaces, - network_handler=network_handler) - return None, None, None - - if server_grp_id_result and not server_grp_id_result.get('result'): - LOG.error('Validation failed for Nova anti-affinity ' - 'server group.') - return None, None, None - - provider_metadata = provider_metadata_result.get('result', None) - if not provider_metadata: - LOG.warning('Failed to get provider metadata for' - ' device creation.') - provider_metadata = {} - - return interfaces, image_id, provider_metadata - - def _get_service_instance_flavor(self, device_data): - if device_data['service_details'].get('flavor'): - flavor = device_data['service_details']['flavor'] - else: - LOG.debug("No Device flavor provided in service profile's " - "service flavor field, using default " - "flavor: m1.medium") - flavor = 'm1.medium' - return flavor - - def _update_interfaces_for_non_hotplug_support(self, network_handler, - interfaces, - interfaces_to_attach, - device_data): - token = device_data['token'] - enable_port_security = device_data.get('enable_port_security') - if not device_data['interfaces_to_attach']: - for port in device_data['ports']: - if (port['port_classification'] == - nfp_constants.PROVIDER): - if (device_data['service_details'][ - 'service_type'].lower() - in [nfp_constants.FIREWALL.lower(), - nfp_constants.VPN.lower()]): - network_handler.set_promiscuos_mode( - token, port['id'], enable_port_security) - port_id = network_handler.get_port_id( - token, port['id']) - interfaces_to_attach.append({'port': port_id}) - for port in device_data['ports']: - if (port['port_classification'] == - nfp_constants.CONSUMER): - if (device_data['service_details'][ - 'service_type'].lower() - in [nfp_constants.FIREWALL.lower(), - nfp_constants.VPN.lower()]): - network_handler.set_promiscuos_mode( - token, port['id'], enable_port_security) - port_id = network_handler.get_port_id( - token, port['id']) - interfaces_to_attach.append({'port': port_id}) - else: - for interface in device_data['interfaces_to_attach']: - interfaces_to_attach.append( - {'port': interface['port']}) - interfaces.append({'id': interface['id']}) - - def _validate_create_instance_executor_results(self, - network_handler, - device_data, - interfaces, - instance_id_result, - port_details_result): - token = device_data['token'] - admin_tenant_id = device_data['admin_tenant_id'] - instance_id = instance_id_result.get('result', None) - if not instance_id: - LOG.error('Failed to create instance with device data:' - '%(data)s.', - {'data': device_data}) - self._delete_interfaces(device_data, interfaces, - network_handler=network_handler) - return None, None - - mgmt_neutron_port_info = port_details_result.get('result', None) - - if not mgmt_neutron_port_info: - LOG.error('Failed to get management port details. ') - with nfp_ctx_mgr.NovaContextManager as ncm: - ncm.retry(self.compute_handler_nova.delete_instance, - token, - admin_tenant_id, - instance_id) - self._delete_interfaces(device_data, interfaces, - network_handler=network_handler) - return None, None - return instance_id, mgmt_neutron_port_info - - @_set_network_handler - def delete_network_function_device(self, device_data, - network_handler=None): - """ Delete the NFD - - :param device_data: NFD - :type device_data: dict - - :returns: None -- Both on success and Failure - - :raises: exceptions.IncompleteData, - exceptions.ComputePolicyNotSupported - """ - if ( - any(key not in device_data - for key in ['service_details', - 'mgmt_port_id']) or - - type(device_data['service_details']) is not dict or - - any(key not in device_data['service_details'] - for key in ['service_vendor', - 'device_type', - 'network_mode']) or - - type(device_data['mgmt_port_id']) is not dict or - - any(key not in device_data['mgmt_port_id'] - for key in ['id', - 'port_classification', - 'port_model']) - ): - raise exceptions.IncompleteData() - - if ( - device_data['service_details']['device_type'] != - nfp_constants.NOVA_MODE - ): - raise exceptions.ComputePolicyNotSupported( - compute_policy=device_data['service_details']['device_type']) - - token = self._get_token(device_data.get('token')) - - if not token: - return None - - if device_data.get('id'): - # delete the device instance - # - # this method will be invoked again - # once the device instance deletion is completed - with nfp_ctx_mgr.NovaContextManager.new( - suppress=(Exception,)) as ncm: - - ncm.retry(self.compute_handler_nova.delete_instance, - token, - device_data['tenant_id'], - device_data['id']) - else: - # device instance deletion is done, delete remaining resources - try: - interfaces = [device_data['mgmt_port_id']] - self._delete_interfaces(device_data, - interfaces, - network_handler=network_handler) - except Exception as e: - LOG.error('Failed to delete the management data port(s). ' - 'Error: %(error)s', {'error': e}) - - def get_network_function_device_status(self, device_data, - ignore_failure=False): - """ Get the status of NFD - - :param device_data: NFD - :type device_data: dict - - :returns: None -- On failure - :return: str -- status string - - :raises: exceptions.IncompleteData, - exceptions.ComputePolicyNotSupported - """ - if ( - any(key not in device_data - for key in ['id', - 'service_details']) or - - type(device_data['service_details']) is not dict or - - any(key not in device_data['service_details'] - for key in ['service_vendor', - 'device_type', - 'network_mode']) - ): - raise exceptions.IncompleteData() - - if ( - device_data['service_details']['device_type'] != - nfp_constants.NOVA_MODE - ): - raise exceptions.ComputePolicyNotSupported( - compute_policy=device_data['service_details']['device_type']) - - token = self._get_token(device_data.get('token')) - - if not token: - return None - - with nfp_ctx_mgr.NovaContextManager.new(suppress=(Exception,)) as ncm: - device = ncm.retry(self.compute_handler_nova.get_instance, - device_data['token'], - device_data['tenant_id'], - device_data['id']) - - return device['status'] - - @_set_network_handler - def plug_network_function_device_interfaces(self, device_data, - network_handler=None): - """ Attach the network interfaces for NFD - - :param device_data: NFD - :type device_data: dict - - :returns: bool -- False on failure and True on Success - - :raises: exceptions.IncompleteData, - exceptions.ComputePolicyNotSupported - """ - - if ( - any(key not in device_data - for key in ['id', - 'service_details', - 'ports']) or - - type(device_data['service_details']) is not dict or - - any(key not in device_data['service_details'] - for key in ['service_vendor', - 'device_type', - 'network_mode']) or - - type(device_data['ports']) is not list or - - any(key not in port - for port in device_data['ports'] - for key in ['id', - 'port_classification', - 'port_model']) - ): - raise exceptions.IncompleteData() - - if ( - device_data['service_details']['device_type'] != - nfp_constants.NOVA_MODE - ): - raise exceptions.ComputePolicyNotSupported( - compute_policy=device_data['service_details']['device_type']) - - token = device_data['token'] - tenant_id = device_data['tenant_id'] - provider_metadata = device_data['provider_metadata'] - enable_port_security = device_data.get('enable_port_security') - - if provider_metadata.get('supports_hotplug') is False: - return True - try: - executor = nfp_executor.TaskExecutor(jobs=10) - - for port in device_data['ports']: - if port['port_classification'] == nfp_constants.PROVIDER: - service_type = device_data[ - 'service_details']['service_type'].lower() - if service_type.lower() in [ - nfp_constants.FIREWALL.lower(), - nfp_constants.VPN.lower()]: - executor.add_job( - 'SET_PROMISCUOS_MODE', - network_handler.set_promiscuos_mode_fast, - token, port['id'], enable_port_security) - executor.add_job( - 'ATTACH_INTERFACE', - self.compute_handler_nova.attach_interface, - token, tenant_id, device_data['id'], - port['id']) - break - executor.fire() - - for port in device_data['ports']: - if port['port_classification'] == nfp_constants.CONSUMER: - service_type = device_data[ - 'service_details']['service_type'].lower() - if service_type.lower() in [ - nfp_constants.FIREWALL.lower(), - nfp_constants.VPN.lower()]: - executor.add_job( - 'SET_PROMISCUOS_MODE', - network_handler.set_promiscuos_mode_fast, - token, port['id'], enable_port_security) - executor.add_job( - 'ATTACH_INTERFACE', - self.compute_handler_nova.attach_interface, - token, tenant_id, device_data['id'], - port['id']) - break - executor.fire() - - except Exception as e: - LOG.error('Failed to plug interface(s) to the device.' - 'Error: %(error)s', {'error': e}) - return None - else: - return True - - @_set_network_handler - def unplug_network_function_device_interfaces(self, device_data, - network_handler=None): - """ Detach the network interfaces for NFD - - :param device_data: NFD - :type device_data: dict - - :returns: bool -- False on failure and True on Success - - :raises: exceptions.IncompleteData, - exceptions.ComputePolicyNotSupported - """ - - if ( - any(key not in device_data - for key in ['id', - 'service_details', - 'ports']) or - - type(device_data['service_details']) is not dict or - - any(key not in device_data['service_details'] - for key in ['service_vendor', - 'device_type', - 'network_mode']) or - - any(key not in port - for port in device_data['ports'] - for key in ['id', - 'port_classification', - 'port_model']) - ): - raise exceptions.IncompleteData() - - if ( - device_data['service_details']['device_type'] != - nfp_constants.NOVA_MODE - ): - raise exceptions.ComputePolicyNotSupported( - compute_policy=device_data['service_details']['device_type']) - - token = self._get_token(device_data.get('token')) - - if not token: - return None - - image_name = self._get_image_name(device_data) - provider_metadata = {} - if image_name: - provider_metadata = ( - self._update_provider_metadata_fast(token, - device_data['tenant_id'], - image_name, - device_data)) - - if not provider_metadata: - LOG.debug('Failed to get provider metadata for' - ' device deletion.') - - if provider_metadata.get('supports_hotplug') is False: - return True - - with nfp_ctx_mgr.NovaContextManager.new(suppress=(Exception,)) as ncm: - for port in device_data['ports']: - port_id = self._get_port_from_pt(device_data, port['id']) - ncm.retry(self.compute_handler_nova.detach_interface, - token, - device_data['tenant_id'], - device_data['id'], - port_id) - # Async change - self._delete_port(token, port_id) - # Async change: Delete stale l2ps - try: - self._delete_l2ps(token, device_data, network_handler) - except Exception: - pass - return True - - def _delete_l2ps(self, token, device_data, network_handler): - ''' - delete l2 policies - ''' - gbp_cli = network_handler.network_handler - for ptg in device_data['provider']['ptg']: - try: - ptg_details = gbp_cli.get_policy_target_group(token, - ptg['id']) - if ptg_details: - LOG.debug('Provider ptg is in use !!') - continue - except Exception: - LOG.debug('Provider not found !!') - with nfp_ctx_mgr.GBPContextManager as gcm: - l2p = gbp_cli.get_l2_policy(token, ptg['l2_policy_id']) - # deleting l2p if it is created implicitly - if 'Implicitly' in l2p['description']: - gcm.retry(gbp_cli.delete_l2_policy, token, - ptg['l2_policy_id']) - - if ('consumer' not in list(device_data.keys()) or not device_data[ - 'consumer'].get('ptg')): - return - - for ptg in device_data['consumer']['ptg']: - try: - ptg_details = gbp_cli.get_policy_target_group(token, - ptg['id']) - if ptg_details: - LOG.debug('Stitching PTG is in use !!') - continue - except Exception: - LOG.debug('Stitching PTG not found !!') - with nfp_ctx_mgr.GBPContextManager as gcm: - l2p = gbp_cli.get_l2_policy(token, ptg['l2_policy_id']) - # deleting l2p if it is created implicitly - if 'Implicitly' in l2p['description']: - gcm.retry(gbp_cli.delete_l2_policy, token, - ptg['l2_policy_id']) - - def _delete_port(self, token, port_id): - ''' - delete neutron port - ''' - try: - network_handler = self.network_handlers[nfp_constants.NEUTRON_MODE] - network_handler.delete_port(token, port_id) - except Exception as exc: - LOG.error("Failed to delete port %(port_id)s. Error: %(exc)s", - {"port_id": port_id, 'exc': exc}) - - def _get_port_from_pt(self, device_data, pt_id): - ''' - get neutron_port_id from pt_id using data - ''' - port_id = None - for pt in device_data['provider']['pt']: - if pt['id'] == pt_id: - return pt['port_id'] - - if not device_data['consumer'].get('pt'): - return port_id - - for pt in device_data['consumer']['pt']: - if pt['id'] == pt_id: - return pt['port_id'] - LOG.error('Policy Target %(pt_id) not found in provided data', - {'pt_id': pt_id}) - return port_id - - def get_port_details(self, port_id, port_model, data): - ''' - function to retrieve port details from data - ''' - if 'gbp' in port_model: - for pt in data['pt']: - if port_id == pt['id']: - port_id = pt['port_id'] - break - - for port in data['port']: - if port_id == port['id']: - ip = port['fixed_ips'][0]['ip_address'] - mac = port['mac_address'] - cidr = data['subnet']['cidr'] - gateway_ip = data['subnet']['gateway_ip'] - return (ip, mac, cidr, gateway_ip, port, data['subnet']) - - @_set_network_handler - def get_delete_device_data(self, device_data, network_handler=None, - devices_data=None): - """ Get the configuration information for NFD - - :param device_data: NFD - :type device_data: dict - - :param devices_data: metadata of NFD and NF - :type device_data: dict - - :returns: None -- On Failure - :returns: dict - - :raises: exceptions.IncompleteData - - """ - - if ( - any(key not in device_data - for key in ['service_details', - 'mgmt_ip_address', - 'ports']) or - - type(device_data['service_details']) is not dict or - - any(key not in device_data['service_details'] - for key in ['service_vendor', - 'device_type', - 'network_mode']) or - - type(device_data['ports']) is not list or - - any(key not in port - for port in device_data['ports'] - for key in ['id', - 'port_classification', - 'port_model']) - ): - LOG.error('Incomplete device data received for delete ' - 'network function device.') - return None - - token = self._get_token(device_data.get('token')) - if not token: - return None - - provider_ip = None - provider_mac = None - provider_cidr = None - consumer_ip = None - consumer_mac = None - consumer_cidr = None - consumer_gateway_ip = None - - for port in device_data['ports']: - if port['port_classification'] == nfp_constants.PROVIDER: - try: - (provider_ip, provider_mac, provider_cidr, dummy, _, _) = ( - self.get_port_details(port['id'], port['port_model'], - devices_data['provider']) - ) - except Exception: - LOG.error('Failed to get provider port details' - ' for get device config info operation') - return None - elif port['port_classification'] == nfp_constants.CONSUMER: - try: - (consumer_ip, consumer_mac, consumer_cidr, - consumer_gateway_ip, _, _) = ( - self.get_port_details(port['id'], port['port_model'], - devices_data['consumer']) - ) - except Exception: - LOG.error('Failed to get consumer port details' - ' for get device config info operation') - return None - - device_data.update({ - 'provider_ip': provider_ip, 'provider_mac': provider_mac, - 'provider_cidr': provider_cidr, 'consumer_ip': consumer_ip, - 'consumer_mac': consumer_mac, 'consumer_cidr': consumer_cidr, - 'consumer_gateway_ip': consumer_gateway_ip}) - - return device_data - - @_set_network_handler - def get_network_function_device_config(self, device_data, - resource_type, is_delete=False, - network_handler=None, - devices_data=None): - """ Get the configuration information for NFD - - :returns: dict - - """ - - if is_delete: - device_data = self.get_delete_device_data( - device_data, network_handler=network_handler, - devices_data=devices_data) - if not device_data: - return None - - return df.get_network_function_info( - device_data, resource_type) diff --git a/gbpservice/nfp/orchestrator/modules/device_orchestrator.py b/gbpservice/nfp/orchestrator/modules/device_orchestrator.py deleted file mode 100644 index 66a395531..000000000 --- a/gbpservice/nfp/orchestrator/modules/device_orchestrator.py +++ /dev/null @@ -1,1880 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import copy -import sys -import traceback - -from neutron_lib import context as n_context -from neutron_lib import rpc as n_rpc -import oslo_messaging as messaging - -from gbpservice._i18n import _ -from gbpservice.neutron.db import api as db_api -from gbpservice.nfp.common import constants as nfp_constants -from gbpservice.nfp.common import topics as nsf_topics -from gbpservice.nfp.common import utils as nfp_utils -from gbpservice.nfp.core import context as module_context -from gbpservice.nfp.core.event import Event -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.core import module as nfp_api -from gbpservice.nfp.core.rpc import RpcAgent -from gbpservice.nfp.lib import nfp_context_manager as nfp_ctx_mgr -from gbpservice.nfp.lib import transport -from gbpservice.nfp.orchestrator.db import nfp_db as nfp_db -from gbpservice.nfp.orchestrator.drivers import orchestration_driver -from gbpservice.nfp.orchestrator.openstack import openstack_driver - - -LOG = nfp_logging.getLogger(__name__) - -STOP_POLLING = {'poll': False} -CONTINUE_POLLING = {'poll': True} - - -def rpc_init(controller, config): - rpcmgr = RpcHandler(config, controller) - agent = RpcAgent( - controller, - host=config.host, - topic=nsf_topics.NFP_CONFIGURATOR_NDO_TOPIC, - manager=rpcmgr) - controller.register_rpc_agents([agent]) - - -def events_init(controller, config, device_orchestrator): - events = ['CREATE_NETWORK_FUNCTION_DEVICE', 'DEVICE_SPAWNING', - 'DEVICE_HEALTHY', 'HEALTH_MONITOR_COMPLETE', - 'DEVICE_UP', - 'CONFIGURE_DEVICE', 'CREATE_DEVICE_CONFIGURATION', - 'CONFIGURATION_COMPLETE', - 'DEVICE_CONFIGURED', "DELETE_CONFIGURATION", - 'DELETE_NETWORK_FUNCTION_DEVICE', - 'DELETE_CONFIGURATION_COMPLETED', - 'DEVICE_BEING_DELETED', - 'DEVICE_NOT_REACHABLE', - 'DEVICE_CONFIGURATION_FAILED', - 'PLUG_INTERFACES', 'UNPLUG_INTERFACES', - 'UPDATE_DEVICE_CONFIG_PARAMETERS', - 'DEVICE_CONFIG_PARAMETERS_UPDATED', - 'PERIODIC_HM_DEVICE_REACHABLE', - 'PERIODIC_HM_DEVICE_NOT_REACHABLE', - 'PERFORM_INITIAL_HEALTH_CHECK', - 'PERFORM_PERIODIC_HEALTH_CHECK', - 'PERFORM_CLEAR_HM'] - events_to_register = [] - for event in events: - events_to_register.append( - Event(id=event, handler=device_orchestrator)) - controller.register_events(events_to_register) - - -def nfp_module_init(controller, config): - events_init(controller, config, DeviceOrchestrator(controller, config)) - rpc_init(controller, config) - LOG.debug("Device Orchestrator: module_init") - - -class RpcHandler(object): - RPC_API_VERSION = '1.0' - - def __init__(self, conf, controller): - super(RpcHandler, self).__init__() - self.conf = conf - self._controller = controller - self.rpc_event_mapping = { - nfp_constants.HEALTHMONITOR_RESOURCE: [ - 'HEALTH_MONITOR_COMPLETE', - 'DEVICE_NOT_REACHABLE', - 'DEVICE_NOT_REACHABLE', - 'PERIODIC_HM_DEVICE_REACHABLE', - 'PERIODIC_HM_DEVICE_NOT_REACHABLE', ], - nfp_constants.GENERIC_CONFIG: [ - 'DEVICE_CONFIGURED', - 'DELETE_CONFIGURATION_COMPLETED', - 'DEVICE_CONFIGURATION_FAILED'], - } - - def _log_event_created(self, event_id, event_data): - NFD = event_data.get('network_function_device_id') - NF = event_data.get('network_function_id') - NFI = event_data.get('network_function_instance_id') - - if NFD and NF and NFI: - LOG.info("Created event %(event_name)s with" - " NF:%(nf)s ,NFI:%(nfi)s and NFD:%(nfd)s", - {'event_name': event_id, - 'nf': NF, - 'nfi': NFI, - 'nfd': NFD}) - else: - LOG.info("Created event %(event_name)s ", - {'event_name': event_id}) - - def _create_event(self, event_id, event_data=None, key=None, - is_poll_event=False, original_event=False, max_times=10): - if is_poll_event: - ev = self._controller.new_event( - id=event_id, data=event_data, - serialize=original_event.sequence, - binding_key=original_event.binding_key, - key=original_event.desc.uuid) - LOG.debug("poll event started for %s", ev.id) - self._controller.poll_event(ev, max_times=10) - else: - ev = self._controller.new_event( - id=event_id, - key=key, - data=event_data) - self._controller.post_event(ev) - self._log_event_created(event_id, event_data) - - def handle_periodic_hm_resource(self, result): - if result == nfp_constants.SUCCESS: - event_id = self.rpc_event_mapping[ - nfp_constants.HEALTHMONITOR_RESOURCE][3] - else: - event_id = self.rpc_event_mapping[ - nfp_constants.HEALTHMONITOR_RESOURCE][4] - return event_id - - # RPC APIs status notification from Configurator - def network_function_notification(self, context, notification_data): - nfp_context = module_context.init() - info = notification_data.get('info') - responses = notification_data.get('notification') - request_info = info.get('context') - operation = request_info.get('operation') - logging_context = request_info.get('logging_context', {}) - # nfp_context = request_info.get('nfp_context') - nfp_context['log_context'] = logging_context - if 'nfp_context' in request_info: - nfp_context.update(request_info['nfp_context']) - - for response in responses: - resource = response.get('resource') - data = response.get('data') - result = data.get('status_code') - if resource not in [nfp_constants.HEALTHMONITOR_RESOURCE, - nfp_constants.PERIODIC_HM]: - resource = nfp_constants.GENERIC_CONFIG - - is_delete_request = True if operation == 'delete' else False - - if resource == nfp_constants.PERIODIC_HM: - event_id = self.handle_periodic_hm_resource(result) - break - - if is_delete_request: - event_id = self.rpc_event_mapping[resource][1] - else: - event_id = self.rpc_event_mapping[resource][0] - - if result.lower() != 'success': - LOG.info("RPC Handler response data:%(data)s", - {'data': data}) - if is_delete_request: - # Ignore any deletion errors, generate SUCCESS event - event_id = self.rpc_event_mapping[resource][1] - else: - event_id = self.rpc_event_mapping[resource][2] - break - - nf_id = request_info.pop('nf_id') - nfi_id = request_info.pop('nfi_id') - nfd_id = request_info.pop('nfd_id') - request_info['network_function_id'] = nf_id - request_info['network_function_instance_id'] = nfi_id - request_info['network_function_device_id'] = nfd_id - event_data = request_info - event_data['id'] = request_info['network_function_device_id'] - - key = nf_id - self._create_event(event_id=event_id, - event_data=event_data, - key=key) - - -class DeviceOrchestrator(nfp_api.NfpEventHandler): - """device Orchestrator For Network Services - - This class handles the orchestration of Network Function Device lifecycle. - It deals with physical service resources - Network Devices. This module - interacts with Service Orchestrator and Configurator. Service Orchestrator - sends device create/delete request, Device orchestrator sends/receieves - RPC to/from configurator to create generic config. Device Orchestrator - loads drivers specified in config file, and selects corresponding drivers - based on service vendor. - - Workflow for create: - 1) Service Orchestarator calls Device Orcehstrator(NDO) for creating new - device, create_network_function_device gets called in the context of - event handler. This method checks with drivers for device sharing, if - device sharing supported then request the driver to plug the - interfaces(step-3) else request driver to create a new device and poll - for its status(Here a DB entry is created with status as PENDING_CREATE). - 2) If the driver returns status as ACTIVE or ERROR, the poll event is - stopped, if it returns any other status, the poll event is continued. - 2) In case of new device creation, once device become ACTIVE, NDO plug - the interfaces. - 3) After plugging the interfaces NDO sends RPC call to configurator for - creating generic config. - 4) Rpc Handler receives notification API from configurator, In case of - success update DB with status as ACTIVE and create event DEVICE_CREATED - for Service Orchestrator. In case of any error, create event - DEVICE_CREATE_FAILED and update DB as ERROR. - - """ - - def __init__(self, controller, config): - self._controller = controller - self.config = config - self.nsf_db = nfp_db.NFPDbBase() - self.gbpclient = openstack_driver.GBPClient(config) - self.keystoneclient = openstack_driver.KeystoneClient(config) - - neutron_context = n_context.get_admin_context() - self.configurator_rpc = NDOConfiguratorRpcApi(neutron_context, - self.config) - - self.status_map = { - 'INIT': 'Created Network Service Device with status INIT.', - 'PENDING_CREATE': '', - 'DEVICE_SPAWNING': ('Creating NFD, launched the new device, ' + - 'polling on its status'), - 'DEVICE_UP': 'Device is UP/ACTIVE', - 'PERFORM_HEALTH_CHECK': 'perform health check of service vm', - 'HEALTH_CHECK_PENDING': ('Device health check is going on ' + - ' through configurator'), - 'HEALTH_CHECK_COMPLETED': 'Health check succesfull for device', - 'INTERFACES_PLUGGED': 'Interfaces Plugging successfull', - 'PENDING_CONFIGURATION_CREATE': ('Started configuring device ' + - 'for routes, license, etc'), - 'DEVICE_READY': 'Device is ready to use', - 'ACTIVE': 'Device is Active.', - 'DEVICE_NOT_UP': 'Device not became UP/ACTIVE', - } - self.orchestration_driver = orchestration_driver.OrchestrationDriver( - self.config) - - @property - def db_session(self): - return db_api.get_writer_session() - - def event_method_mapping(self, event_id): - event_handler_mapping = { - "CREATE_NETWORK_FUNCTION_DEVICE": ( - self.create_network_function_device), - "PERFORM_INITIAL_HEALTH_CHECK": self.perform_initial_health_check, - "PERFORM_PERIODIC_HEALTH_CHECK": - self.perform_periodic_health_check, - "PERFORM_CLEAR_HM": self.perform_clear_hm, - "DEVICE_UP": self.device_up, - "PLUG_INTERFACES": self.plug_interfaces_fast, - "DEVICE_HEALTHY": self.plug_interfaces, - "HEALTH_MONITOR_COMPLETE": self.health_monitor_complete, - "CONFIGURE_DEVICE": self.configure_device, - "CREATE_DEVICE_CONFIGURATION": self.create_device_configuration, - "CONFIGURATION_COMPLETE": self.configuration_complete, - "DEVICE_CONFIGURED": self.device_configuration_complete, - - "DELETE_NETWORK_FUNCTION_DEVICE": ( - self.delete_network_function_device), - "DELETE_CONFIGURATION_COMPLETED": ( - self.delete_configuration_complete), - "UNPLUG_INTERFACES": self.unplug_interfaces, - "DELETE_DEVICE": self.delete_device, - "DELETE_CONFIGURATION": self.delete_device_configuration, - "DEVICE_NOT_REACHABLE": self.handle_device_not_reachable, - "PERIODIC_HM_DEVICE_REACHABLE": ( - self.periodic_hm_handle_device_reachable), - "PERIODIC_HM_DEVICE_NOT_REACHABLE": ( - self.periodic_hm_handle_device_not_reachable), - "PLUG_INTERFACE_FAILED": self.handle_plug_interface_failed, - "DEVICE_CONFIGURATION_FAILED": self.handle_device_config_failed, - "DEVICE_ERROR": self.handle_device_create_error, - "DEVICE_NOT_UP": self.handle_device_not_up, - "DRIVER_ERROR": self.handle_driver_error, - 'UPDATE_DEVICE_CONFIG_PARAMETERS': self.update_config_params, - 'DEVICE_CONFIG_PARAMETERS_UPDATED': ( - self.device_configuration_updated) - } - if event_id not in event_handler_mapping: - raise Exception(_("Invalid event ID")) - else: - return event_handler_mapping[event_id] - - def handle_event(self, event): - try: - event_data = event.context - NFD = event_data.get('network_function_device_id') - NF = event_data.get('network_function_id') - NFI = event_data.get('network_function_instance_id') - - if NFD and NF and NFI: - LOG.info("Received event %(event_name)s with " - "NF:%(nf)s ,NFI:%(nfi)s and NFD:%(nfd)s", - {'event_name': event.id, - 'nf': NF, - 'nfi': NFI, - 'nfd': NFD}) - else: - LOG.info("Received event %(event_name)s ", - {'event_name': event.id}) - event_handler = self.event_method_mapping(event.id) - event_handler(event) - except Exception as e: - LOG.error("error in processing event: %(event_id)s for " - "event data %(event_data)s. error: %(error)s", - {'event_id': event.id, 'event_data': event.data, - 'error': e}) - _, _, tb = sys.exc_info() - traceback.print_tb(tb) - raise e - - def handle_exception(self, event, exception): - return ExceptionHandler.handle(self, event, exception) - - # Helper functions - def _log_event_created(self, event_id, event_data): - network_function_instance = event_data.get('network_function_instance') - if network_function_instance: - nf = network_function_instance.get('network_function_id') - nfi = network_function_instance.get('id') - else: - nf = None - nfi = None - if nf and nfi: - LOG.info("Created event %(event_name)s with NF:%(nf)s and " - "NFI:%(nfi)s ", - {'event_name': event_id, - 'nf': nf, - 'nfi': nfi}) - else: - LOG.info("Created event %(event_name)s ", - {'event_name': event_id}) - - def _create_event(self, event_id, event_data=None, - is_poll_event=False, original_event=False, - is_internal_event=False, max_times=10): - if not is_internal_event: - if is_poll_event: - ev = self._controller.new_event( - id=event_id, data=event_data, - serialize=original_event.sequence, - binding_key=original_event.binding_key, - key=original_event.desc.uuid) - LOG.debug("poll event started for %s", ev.id) - self._controller.poll_event(ev, max_times=max_times) - else: - ev = self._controller.new_event( - id=event_id, - data=event_data) - self._controller.post_event(ev) - nfp_context = module_context.get() - self._log_event_created(event_id, nfp_context) - else: - # Same module API, so calling corresponding function directly. - nfp_context = module_context.get() - event = self._controller.new_event( - id=event_id, - data=event_data, - context=nfp_context) - self.handle_event(event) - - def _release_cnfd_lock(self, device): - nf_id = device['network_function_id'] - nfi_id = device['network_function_instance_id'] - ev = self._controller.new_event( - id='CREATE_NETWORK_FUNCTION_DEVICE', - data=device, key=nf_id + nfi_id) - if device.get('binding_key'): - ev.binding_key = device.get('binding_key') - LOG.debug("Releasing tenant based lock for " - "CREATE_NETWORK_FUNCTION_DEVICE event with binding " - "key: %s", ev.binding_key) - self._controller.event_complete(ev) - - def event_cancelled(self, ev, reason): - LOG.info("Poll event %(event_id)s cancelled.", - {'event_id': ev.id}) - - if ev.id == 'DEVICE_SPAWNING': - LOG.info("Device is not up still after 10secs of launch") - # create event DEVICE_NOT_UP - device = self._prepare_failure_case_device_data(ev.data) - self._create_event(event_id='DEVICE_NOT_UP', - event_data=device, - is_internal_event=True) - self._update_network_function_device_db(device, - 'DEVICE_NOT_UP') - if ev.id == 'DEVICE_BEING_DELETED': - LOG.info("Device is not deleted completely." - " Continuing further cleanup of resources." - " Possibly there could be stale port resources" - " on Compute") - device = ev.data - orchestration_driver = self._get_orchestration_driver( - device['service_details']['service_vendor']) - device_id = device['id'] - del device['id'] - nf_id = device['network_function_id'] - orchestration_driver.delete_network_function_device(device) - self._delete_network_function_device_db(device_id, device) - dnfd_event = ( - self._controller.new_event(id='DELETE_NETWORK_FUNCTION_DEVICE', - key=nf_id, - binding_key=nf_id, - desc_dict=device.get( - 'event_desc'))) - self._controller.event_complete(dnfd_event, result='FAILED') - - def _update_device_status(self, device, state, status_desc=None): - device['status'] = state - if status_desc: - device['status_description'] = status_desc - else: - device['status_description'] = self.status_map.get(state) - - def _get_port(self, port_id): - with nfp_ctx_mgr.DbContextManager: - return self.nsf_db.get_port_info(self.db_session, port_id) - - def _get_ports(self, port_ids): - data_ports = [] - for port_id in port_ids: - with nfp_ctx_mgr.DbContextManager: - port_info = self.nsf_db.get_port_info(self.db_session, - port_id) - data_ports.append(port_info) - return data_ports - - def _create_network_function_device_db(self, device_info, state): - - self._update_device_status(device_info, state) - # (ashu) driver should return device_id as vm_id - device_id = device_info.pop('id') - device_info['id'] = device_id - device_info['reference_count'] = 0 - device_info['interfaces_in_use'] = 0 - with nfp_ctx_mgr.DbContextManager: - device = self.nsf_db.create_network_function_device( - self.db_session, - device_info) - return device - - def _update_network_function_device_db(self, device, state, - status_desc=''): - self._update_device_status(device, state, status_desc) - updated_device = copy.deepcopy(device) - updated_device.pop('reference_count', None) - updated_device.pop('interfaces_in_use', None) - with nfp_ctx_mgr.DbContextManager: - self.nsf_db.update_network_function_device(self.db_session, - updated_device['id'], - updated_device) - device.update(updated_device) - - def _delete_network_function_device_db(self, device_id, device): - with nfp_ctx_mgr.DbContextManager: - self.nsf_db.delete_network_function_device(self.db_session, - device_id) - - def _get_network_function_info(self, device_id): - nfi_filters = {'network_function_device_id': [device_id]} - with nfp_ctx_mgr.DbContextManager: - network_function_instances = ( - self.nsf_db.get_network_function_instances(self.db_session, - nfi_filters)) - network_function_ids = [nf['network_function_id'] - for nf in network_function_instances] - network_functions = ( - self.nsf_db.get_network_functions( - self.db_session, - {'id': network_function_ids})) - return network_functions - - def _get_network_function_devices(self, filters=None): - with nfp_ctx_mgr.DbContextManager: - network_function_devices = ( - self.nsf_db.get_network_function_devices(self.db_session, - filters)) - for device in network_function_devices: - mgmt_port_id = device.pop('mgmt_port_id') - mgmt_port_id = self._get_port(mgmt_port_id) - device['mgmt_port_id'] = mgmt_port_id - - network_functions = ( - self._get_network_function_info(device['id'])) - device['network_functions'] = network_functions - return network_function_devices - - def _increment_device_ref_count(self, device): - with nfp_ctx_mgr.DbContextManager: - self.nsf_db.increment_network_function_device_count( - self.db_session, - device['id'], - 'reference_count') - device['reference_count'] += 1 - - def _decrement_device_ref_count(self, device): - with nfp_ctx_mgr.DbContextManager: - self.nsf_db.decrement_network_function_device_count( - self.db_session, - device['id'], - 'reference_count') - device['reference_count'] -= 1 - - def _increment_device_interface_count(self, device): - with nfp_ctx_mgr.DbContextManager: - self.nsf_db.increment_network_function_device_count( - self.db_session, - device['id'], - 'interfaces_in_use', - len(device['ports'])) - - device['interfaces_in_use'] += len(device['ports']) - - def _decrement_device_interface_count(self, device): - with nfp_ctx_mgr.DbContextManager: - self.nsf_db.decrement_network_function_device_count( - self.db_session, - device['id'], - 'interfaces_in_use', - len(device['ports'])) - - device['interfaces_in_use'] -= len(device['ports']) - - def _get_orchestration_driver(self, service_vendor): - return self.orchestration_driver - - def _get_device_data(self, nfd_request): - - device_data = {} - network_function = nfd_request.get('network_function') - network_function_instance = nfd_request['network_function_instance'] - service_details = nfd_request['service_details'] - device_data['name'] = network_function_instance['name'] - device_data['share_existing_device'] = ( - nfd_request.get('share_existing_device')) - device_data['management_network_info'] = ( - nfd_request.get('management_network_info')) - - if network_function: - device_data['network_function_id'] = network_function['id'] - device_data['service_chain_id'] = ( - network_function['service_chain_id']) - - device_data['network_function_instance_id'] = ( - network_function_instance['id']) - device_data['tenant_id'] = network_function_instance['tenant_id'] - - nsi_port_info = [] - for port_id in network_function_instance.pop('port_info'): - with nfp_ctx_mgr.DbContextManager: - port_info = self.nsf_db.get_port_info(self.db_session, - port_id) - nsi_port_info.append(port_info) - - device_data['ports'] = nsi_port_info - - device_data['service_details'] = service_details - if nsi_port_info[0]['port_model'] == nfp_constants.GBP_PORT: - device_data['service_details']['network_mode'] = ( - nfp_constants.GBP_MODE) - else: - device_data['service_details']['network_mode'] = ( - nfp_constants.NEUTRON_MODE) - device_data['service_vendor'] = service_details['service_vendor'] - - return device_data - - def _get_nsf_db_resource(self, resource_name, resource_id): - db_method = getattr(self.nsf_db, 'get_' + resource_name) - return db_method(self.db_session, resource_id) - - def _update_device_data(self, device, device_data): - device.update(device_data) - return device - - def _make_ports_dict(self, consumer, provider, port_type): - - t_ports = [] - for ptg in [consumer, provider]: - if (port_type in list(ptg.keys())) and ptg[port_type]: - t_ports.append({ - 'id': ptg[port_type].get('id'), - 'port_classification': ptg.get( - 'port_classification'), - 'port_model': ptg.get('port_model') - }) - return t_ports - - def _prepare_device_data_from_nfp_context(self, nfp_context): - device_data = {} - - network_function = nfp_context['network_function'] - network_function_instance = nfp_context['network_function_instance'] - service_details = nfp_context['service_details'] - - device_data['token'] = nfp_context[ - 'resource_owner_context']['admin_token'] - device_data['admin_tenant_id'] = nfp_context[ - 'resource_owner_context']['admin_tenant_id'] - device_data['name'] = network_function_instance['name'] - device_data['share_existing_device'] = nfp_context[ - 'share_existing_device'] - - management_network_info = { - 'id': nfp_context['management_ptg_id'], - 'port_model': nfp_constants.GBP_NETWORK - } - - consumer = nfp_context['consumer'] - provider = nfp_context['provider'] - - ports = self._make_ports_dict(nfp_context.get( - 'explicit_consumer', consumer), provider, 'pt') - - device_data['provider_name'] = provider['ptg']['name'] - device_data['management_network_info'] = management_network_info - - device_data['network_function_id'] = network_function['id'] - device_data['service_chain_id'] = network_function['service_chain_id'] - device_data[ - 'network_function_instance_id'] = network_function_instance['id'] - device_data['tenant_id'] = network_function_instance['tenant_id'] - device_data['ports'] = ports - device_data['service_details'] = service_details - device_data['service_details']['network_mode'] = nfp_constants.GBP_MODE - device_data['service_vendor'] = service_details['service_vendor'] - device_data['server_grp_id'] = nfp_context.get('server_grp_id') - device_data['interfaces_to_attach'] = ( - nfp_context.get('interfaces_to_attach')) - - if nfp_context.get('files'): - device_data['files'] = nfp_context['files'] - if nfp_context.get('user_data'): - device_data['user_data'] = nfp_context['user_data'] - return device_data - - def _create_nfd_entry(self, nfp_context, driver_device_info, - device_data, service_details): - nfp_context['provider_metadata'] = driver_device_info.get( - 'provider_metadata') - # Update nfp_context management with newly created mgmt port - management = nfp_context['management'] - management['port'] = driver_device_info[ - 'mgmt_neutron_port_info']['neutron_port'] - management['port']['ip_address'] = management[ - 'port']['fixed_ips'][0]['ip_address'] - management['subnet'] = driver_device_info[ - 'mgmt_neutron_port_info']['neutron_subnet'] - - # Update newly created device with required params - device = self._update_device_data(driver_device_info, device_data) - device['network_function_device_id'] = device['id'] - - # check for any explicit interface and its type. - for interface in nfp_context.get('explicit_interfaces', []): - if interface['type'] == 'gateway': - device['gateway_port'] = interface['port'] - - name = '%s_%s_%s_%s' % ( - device['provider_name'], - service_details['service_type'], - nfp_context['resource_owner_context']['tenant_name'][:6], - device['network_function_device_id'][:3]) - device['name'] = name - # Create DB entry with status as DEVICE_SPAWNING - network_function_device = ( - self._create_network_function_device_db(device, - 'DEVICE_SPAWNING')) - - nfp_context['network_function_device'] = network_function_device - return device - - def _update_nfp_context_with_ports(self, nfp_context, device): - # REVISIT(mak) Wrong but nfp_db method needs in this format - network_function_device = nfp_context['network_function_device'] - network_function_device['mgmt_port_id'] = device['mgmt_port_id'] - - def _post_create_nfd_events(self, event, nfp_context, device): - - nfp_context['event_desc'] = event.desc.to_dict() - # Updating nfi with nfd_id before device spawning - # to stop orchestration to move further. - nfi = { - 'network_function_device_id': device['id'], - } - with nfp_ctx_mgr.DbContextManager: - nfi = self.nsf_db.update_network_function_instance( - self.db_session, - device['network_function_instance_id'], nfi) - # This event is act as a dummy event for nfp, - # for non-hotplug sharing it will be used - self._create_event(event_id='DEVICE_CREATED', - event_data=device) - - self._create_event(event_id='DEVICE_SPAWNING', - event_data=nfp_context, - is_poll_event=True, - original_event=event, - max_times=nfp_constants.DEVICE_SPAWNING_MAXRETRY) - - # Create path - def create_network_function_device(self, event): - """ Returns device instance for a new service - - This method either returns existing device which could be reused for a - new service or it creates new device instance - """ - - nfp_context = event.context - nfd_request = self._prepare_failure_case_device_data(nfp_context) - service_details = nfp_context['service_details'] - - LOG.info("Received event CREATE NETWORK FUNCTION " - "DEVICE request.") - - orchestration_driver = self._get_orchestration_driver( - service_details['service_vendor']) - - device_data = self._prepare_device_data_from_nfp_context(nfp_context) - - LOG.info("Creating new device:%(device)s", - {'device': nfd_request}) - device_data['volume_support'] = ( - self.config.device_orchestrator.volume_support) - device_data['volume_size'] = ( - self.config.device_orchestrator.volume_size) - device_data['explicit_interfaces'] = nfp_context.get( - 'explicit_interfaces', []) - driver_device_info = ( - orchestration_driver.create_network_function_device( - device_data)) - if not driver_device_info: - LOG.info("Device creation failed") - self._create_event(event_id='DEVICE_ERROR', - event_data=nfd_request, - is_internal_event=True) - self._controller.event_complete(event) - return None - - device = self._create_nfd_entry(nfp_context, driver_device_info, - device_data, service_details) - self._increment_device_ref_count(device) - self._increment_device_interface_count(device) - nfd_id = device.get('network_function_device_id', - '-') if device else '-' - nfp_context['log_context']['nfd_id'] = nfd_id - self._update_nfp_context_with_ports(nfp_context, driver_device_info) - - self._post_create_nfd_events(event, nfp_context, device) - - def _post_device_up_event_graph(self, nfp_context): - nf_id = nfp_context['network_function']['id'] - nfi_id = nfp_context['network_function_instance']['id'] - du_event = self._controller.new_event(id="DEVICE_UP", - key=nf_id + nfi_id) - - hc_event = self._controller.new_event( - id="PERFORM_INITIAL_HEALTH_CHECK", - key=nf_id + nfi_id) - - plug_int_event = self._controller.new_event(id="PLUG_INTERFACES", - key=nf_id + nfi_id) - GRAPH = ({ - du_event: [hc_event, plug_int_event]}) - - self._controller.post_graph( - GRAPH, du_event, graph_str='HEALTH_MONITOR_GRAPH') - - @nfp_api.poll_event_desc(event='DEVICE_SPAWNING', - spacing=nfp_constants.DEVICE_SPAWNING_SPACING) - def check_device_is_up(self, event): - nfp_context = event.context - - service_details = nfp_context['service_details'] - network_function_device = nfp_context['network_function_device'] - token = nfp_context['resource_owner_context']['admin_token'] - tenant_id = nfp_context['resource_owner_context']['admin_tenant_id'] - - device = { - 'token': token, - 'tenant_id': tenant_id, - 'id': network_function_device['id'], - 'service_details': service_details} - - orchestration_driver = self._get_orchestration_driver( - service_details['service_vendor']) - - is_device_up = ( - orchestration_driver.get_network_function_device_status(device)) - - if is_device_up == nfp_constants.ACTIVE: - LOG.info("Device with NFD:%(id)s came up for " - "tenant:%(tenant)s ", - {'id': network_function_device['id'], - 'tenant': tenant_id}) - self._post_device_up_event_graph(nfp_context) - - return STOP_POLLING - elif is_device_up == nfp_constants.ERROR: - # create event DEVICE_NOT_UP - device = self._prepare_failure_case_device_data(nfp_context) - self._create_event(event_id='DEVICE_NOT_UP', - event_data=device, - is_internal_event=True) - self._update_network_function_device_db(device, - 'DEVICE_NOT_UP') - return STOP_POLLING - else: - # Continue polling until device status became ACTIVE/ERROR. - return CONTINUE_POLLING - - def _post_configure_device_graph(self, nfp_context, serialize=False): - nf_id = nfp_context['network_function']['id'] - nfi_id = nfp_context['network_function_instance']['id'] - sc_instance_id = nfp_context['service_chain_instance']['id'] - binding_key = nfp_context['service_details'][ - 'service_vendor'].lower() + nf_id - device_configure_event = self._controller.new_event( - id='CREATE_DEVICE_CONFIGURATION', - key=nf_id, - serialize=serialize, - binding_key=binding_key) - check_heat_config = self._controller.new_event( - id='SEND_USER_CONFIG', - key=nf_id) - user_config_event = self._controller.new_event( - id='INITIATE_USER_CONFIG', - key=nf_id, - serialize=serialize, - binding_key=binding_key) - device_configured_event = self._controller.new_event( - id='CONFIGURATION_COMPLETE', - key=nf_id, - serialize=serialize, - binding_key=sc_instance_id) - device_periodic_hm_event = self._controller.new_event( - id='PERFORM_PERIODIC_HEALTH_CHECK', - key=nf_id + nfi_id) - - # Start periodic health monitor after device configuration - - GRAPH = ({ - device_periodic_hm_event: [device_configured_event], - device_configured_event: [device_configure_event, - user_config_event], - user_config_event: [check_heat_config]}) - - self._controller.post_graph(GRAPH, device_periodic_hm_event, - graph_str='DEVICE_CONFIGURATION_GRAPH') - - def device_up(self, event, serialize_config=False): - nfp_context = event.context - - # Get the results of PLUG_INTERFACES & PERFORM_INITIAL_HEALTH_CHECK - # events results. - nf_id = nfp_context['network_function']['id'] - nfi_id = nfp_context['network_function_instance']['id'] - device = self._prepare_failure_case_device_data(nfp_context) - # Get the results of PLUG_INTERFACES & PERFORM_INITIAL_HEALTH_CHECK - # events results. - results = event.result - for result in results: - if result.result.lower() != 'success': - # Release CNFD Event lock - self._release_cnfd_lock(device) - self._create_event(event_id='DEVICE_CREATE_FAILED', - event_data=device) - return self._controller.event_complete(event, result='FAILED') - - network_function_device = nfp_context['network_function_device'] - - nfd_id = '-' - if network_function_device: - nfd_id = network_function_device.get('id', '-') - nfp_context['log_context']['nfd_id'] = nfd_id - # Update NFI to ACTIVE State - nfi = { - 'status': nfp_constants.ACTIVE} - nfi = self.nsf_db.update_network_function_instance( - self.db_session, nfi_id, nfi) - self._update_network_function_device_db( - network_function_device, nfp_constants.ACTIVE) - - LOG.info( - "Configuration completed for device with NFD:%(device_id)s. " - "Updated DB status to ACTIVE.", - {'device_id': network_function_device['id']}) - LOG.debug("Device detail:%s", - network_function_device) - # Release CNFD Event lock - self._release_cnfd_lock(device) - self._post_configure_device_graph(nfp_context, - serialize=serialize_config) - event.key = nf_id + nfi_id - self._controller.event_complete(event) - - def prepare_health_check_device_info(self, event, periodicity): - - nfp_context = event.context - - service_details = nfp_context['service_details'] - network_function_device = nfp_context['network_function_device'] - network_function = nfp_context['network_function'] - network_function_instance = nfp_context['network_function_instance'] - mgmt_ip_address = nfp_context['management']['port']['ip_address'] - tenant_id = nfp_context['resource_owner_context']['admin_tenant_id'] - - # The driver tells which protocol / port to monitor ?? - orchestration_driver = self._get_orchestration_driver( - service_details['service_vendor']) - nfp_context['event_desc'] = event.desc.to_dict() - device = { - 'id': network_function_device['id'], - 'tenant_id': tenant_id, - 'mgmt_ip_address': mgmt_ip_address, - 'service_details': service_details, - 'network_function_id': network_function['id'], - 'periodicity': periodicity, - 'network_function_instance_id': network_function_instance['id'], - 'nfp_context': {'event_desc': nfp_context['event_desc'], - 'id': event.id, 'key': event.key}, - } - return device, orchestration_driver - - def perform_clear_hm(self, event): - nfp_context = event.data - network_function = nfp_context['network_function'] - service_details = nfp_context['service_details'] - orchestration_driver = self._get_orchestration_driver( - service_details['service_vendor']) - nfp_context['event_desc'] = event.desc.to_dict() - device = { - 'id': nfp_context['network_function_device_id'], - 'tenant_id': nfp_context['tenant_id'], - 'mgmt_ip_address': nfp_context['mgmt_ip_address'], - 'service_details': service_details, - 'network_function_id': network_function['id'], - 'network_function_instance_id': nfp_context[ - 'network_function_instance_id'], - 'nfp_context': {'event_desc': nfp_context['event_desc'], - 'id': event.id, 'key': event.key}, - } - clear_hm_req = ( - orchestration_driver.get_network_function_device_config( - device, nfp_constants.HEALTHMONITOR_RESOURCE)) - if not clear_hm_req: - self._controller.event_complete(event, result="FAILED") - return None - - self.configurator_rpc.delete_network_function_device_config( - device, - clear_hm_req) - LOG.debug("Clear HM RPC sent to configurator for device: " - "%s with parameters: %s", ( - device['id'], clear_hm_req)) - self._controller.event_complete(event, result="SUCCESS") - - def perform_periodic_health_check(self, event): - event_results = event.result - for result in event_results: - if result.result.lower() != "success": - return self._controller.event_complete(event, result="FAILED") - - device, orchestration_driver = ( - self.prepare_health_check_device_info(event, - nfp_constants.FOREVER)) - hm_req = ( - orchestration_driver.get_network_function_device_config( - device, nfp_constants.HEALTHMONITOR_RESOURCE)) - if not hm_req: - self._controller.event_complete(event, result="FAILED") - return None - - self.configurator_rpc.create_network_function_device_config(device, - hm_req) - LOG.debug("Health Check RPC sent to configurator for device: " - "%s with health check parameters: %s", ( - device['id'], hm_req)) - self._controller.event_complete(event, result="SUCCESS") - - def perform_initial_health_check(self, event): - device, orchestration_driver = ( - self.prepare_health_check_device_info(event, - nfp_constants.INITIAL)) - hm_req = ( - orchestration_driver.get_network_function_device_config( - device, nfp_constants.HEALTHMONITOR_RESOURCE)) - if not hm_req: - self._controller.event_complete(event, result="FAILED") - return None - self.configurator_rpc.create_network_function_device_config(device, - hm_req) - LOG.debug("Health Check RPC sent to configurator for device: " - "%s with health check parameters: %s", ( - device['id'], hm_req)) - - def _get_service_type(self, service_profile_id): - with nfp_ctx_mgr.KeystoneContextManager as kcm: - admin_token = kcm.retry( - self.keystoneclient.get_admin_token, tries=3) - with nfp_ctx_mgr.GBPContextManager as gcm: - service_profile = gcm.retry(self.gbpclient.get_service_profile, - admin_token, service_profile_id) - return service_profile['service_type'].lower() - - def _prepare_device_data(self, device_info): - network_function_id = device_info['network_function_id'] - network_function_device_id = device_info['network_function_device_id'] - network_function_instance_id = ( - device_info['network_function_instance_id']) - - network_function = self._get_nsf_db_resource( - 'network_function', - network_function_id) - network_function_device = self._get_nsf_db_resource( - 'network_function_device', - network_function_device_id) - network_function_instance = self._get_nsf_db_resource( - 'network_function_instance', - network_function_instance_id) - - with nfp_ctx_mgr.KeystoneContextManager as kcm: - admin_token = kcm.retry( - self.keystoneclient.get_admin_token, tries=3) - with nfp_ctx_mgr.GBPContextManager as gcm: - service_profile = gcm.retry( - self.gbpclient.get_service_profile, - admin_token, - network_function['service_profile_id']) - service_details = transport.parse_service_flavor_string( - service_profile['service_flavor']) - - device_info.update({ - 'network_function_instance': network_function_instance}) - device_info.update({'id': network_function_device_id}) - service_details.update({'service_type': self._get_service_type( - network_function['service_profile_id'])}) - device_info.update({'service_details': service_details}) - - device = self._get_device_data(device_info) - device = self._update_device_data(device, network_function_device) - - mgmt_port_id = network_function_device.pop('mgmt_port_id') - mgmt_port_id = self._get_port(mgmt_port_id) - device['mgmt_port_id'] = mgmt_port_id - device['network_function_id'] = network_function_id - - return device - - def _prepare_device_data_fast(self, network_function_details): - network_function = network_function_details['network_function'] - network_function_device = network_function_details[ - 'network_function_device'] - admin_token = network_function_details['admin_token'] - service_profile = network_function_details['service_profile'] - service_details = network_function_details['service_details'] - service_details.update( - {'service_type': service_profile['service_type']}) - device = self._get_device_data(network_function_details) - device = self._update_device_data(device, network_function_device) - mgmt_port_id = network_function_device.pop('mgmt_port_id') - mgmt_port_id = self._get_port(mgmt_port_id) - device['mgmt_port_id'] = mgmt_port_id - device['network_function_id'] = network_function['id'] - device['network_function_device_id'] = ( - network_function_device['id']) - device['token'] = admin_token - device['tenant_id'] = ( - network_function_details['admin_tenant_id']) - device['service_profile'] = service_profile - return device - - def health_monitor_complete(self, event, result='SUCCESS'): - nfp_context = event.data['nfp_context'] - # device = nfp_context['network_function_device'] - # network_function = nfp_context['network_function'] - - # Invoke event_complete for original event which is - # PERFORM_INITIAL_HEALTH_CHECK - event_desc = nfp_context.pop('event_desc', None) - nfp_context.pop('id', None) - key = nfp_context.pop('key', None) - self._controller.event_complete(event) - new_event = self._controller.new_event( - id="PERFORM_INITIAL_HEALTH_CHECK", - key=key, desc_dict=event_desc) - self._controller.event_complete(new_event, result=result) - - def plug_interfaces(self, event, is_event_call=True): - if is_event_call: - device_info = event.data - else: - device_info = event - # Get event data, as configurator sends back only request_info, which - # contains nf_id, nfi_id, nfd_id. - device = self._prepare_device_data(device_info) - self._update_network_function_device_db(device, - 'HEALTH_CHECK_COMPLETED') - orchestration_driver = self._get_orchestration_driver( - device['service_details']['service_vendor']) - - _ifaces_plugged_in = ( - orchestration_driver.plug_network_function_device_interfaces( - device)) - if _ifaces_plugged_in: - self._increment_device_interface_count(device) - self._create_event(event_id='CONFIGURE_DEVICE', - event_data=device, - is_internal_event=True) - else: - self._create_event(event_id='DEVICE_CONFIGURATION_FAILED', - event_data=device, - is_internal_event=True) - - def plug_interfaces_fast(self, event): - - # In this case, the event will be - # happening in parallel with HEALTHMONITORING, - # so, we should not generate CONFIGURE_DEVICE & should not update - # DB with HEALTH_CHECK_COMPLETED. - - nfp_context = event.context - - service_details = nfp_context['service_details'] - network_function_device = nfp_context['network_function_device'] - nf_id = network_function_device['id'] - nfi_id = nfp_context['network_function_instance']['id'] - token = nfp_context['resource_owner_context']['admin_token'] - tenant_id = nfp_context['resource_owner_context']['admin_tenant_id'] - - consumer = nfp_context['consumer'] - provider = nfp_context['provider'] - - event.key = nf_id + nfi_id - - orchestration_driver = self._get_orchestration_driver( - service_details['service_vendor']) - ports = self._make_ports_dict( - nfp_context.get('explicit_consumer', consumer), - provider, 'port') - - device = { - 'id': network_function_device['id'], - 'ports': ports, - 'service_details': service_details, - 'token': token, - 'tenant_id': tenant_id, - 'interfaces_in_use': network_function_device['interfaces_in_use'], - 'status': network_function_device['status'], - 'provider_metadata': nfp_context['provider_metadata'], - 'enable_port_security': nfp_context.get('enable_port_security') - } - - _ifaces_plugged_in = ( - orchestration_driver.plug_network_function_device_interfaces( - device)) - if _ifaces_plugged_in: - # self._increment_device_interface_count(device) - # [REVISIT(mak)] - Check how incremented ref count can be - # updated in DB - self._controller.event_complete(event, result="SUCCESS") - else: - self._create_event(event_id="PLUG_INTERFACE_FAILED", - is_internal_event=True) - self._controller.event_complete(event, result="FAILED") - - def configure_device(self, event): - device = event.data - orchestration_driver = self._get_orchestration_driver( - device['service_details']['service_vendor']) - config_params = ( - orchestration_driver.get_network_function_device_config( - device, nfp_constants.GENERIC_CONFIG)) - if not config_params: - self._create_event(event_id='DRIVER_ERROR', - event_data=device, - is_internal_event=True) - return None - # Sends RPC to configurator to create generic config - self.configurator_rpc.create_network_function_device_config( - device, config_params) - - def create_device_configuration(self, event): - nfp_context = event.context - - service_details = nfp_context['service_details'] - consumer = nfp_context['consumer'] - provider = nfp_context['provider'] - management = nfp_context['management'] - network_function = nfp_context['network_function'] - network_function_instance = nfp_context['network_function_instance'] - network_function_device = nfp_context['network_function_device'] - tenant_id = nfp_context['resource_owner_context']['admin_tenant_id'] - - binding_key = service_details[ - 'service_vendor'].lower() + network_function['id'] - - orchestration_driver = self._get_orchestration_driver( - service_details['service_vendor']) - device = { - 'tenant_id': tenant_id, - 'mgmt_ip_address': management['port']['ip_address'], - 'mgmt_ip': network_function_device['mgmt_ip_address'], - 'provider_ip': provider['port']['ip_address'], - 'provider_cidr': provider['subnet']['cidr'], - 'provider_mac': provider['port']['mac_address'], - 'provider_gateway_ip': provider['subnet']['gateway_ip']} - - if consumer['port'] and consumer['subnet']: - device.update({'consumer_ip': consumer['port']['ip_address'], - 'consumer_cidr': consumer['subnet']['cidr'], - 'consumer_mac': consumer['port']['mac_address'], - 'consumer_gateway_ip': consumer[ - 'subnet']['gateway_ip']}) - - nfp_context['event_desc'] = event.desc.to_dict() - device.update({ - 'id': network_function_device['id'], - 'mgmt_ip_address': network_function_device['mgmt_ip_address'], - 'service_details': service_details, - 'network_function_id': network_function['id'], - 'network_function_instance_id': network_function_instance['id'], - 'nfp_context': { - 'event_desc': nfp_context['event_desc'], - 'id': event.id, 'key': event.key, - 'network_function_device': network_function_device, - 'binding_key': binding_key}}) - - config_params = ( - orchestration_driver. - get_network_function_device_config( - device, nfp_constants.GENERIC_CONFIG)) - - if not config_params: - self._create_event(event_id='DRIVER_ERROR', - event_data=device, - is_internal_event=True) - self._controller.event_complete(event, result="FAILED") - return None - - event_data = {'device': device, 'nfp_context': nfp_context, - 'config_params': config_params} - self._create_event(event_id='UPDATE_DEVICE_CONFIG_PARAMETERS', - event_data=event_data) - - def device_configuration_updated(self, event): - nfp_context, config_params, device = ( - event.data['nfp_context'], event.data['config_params'], - event.data['device']) - # Set forward_route as False in resource_data for configurator to - # handle routes differently, when vpn is in service chain - if nfp_utils.is_vpn_in_service_chain( - nfp_context['service_chain_specs']): - for cfg in config_params['config']: - cfg['resource_data']['forward_route'] = False - else: - for cfg in config_params['config']: - cfg['resource_data']['forward_route'] = True - # Sends RPC to configurator to create generic config - self.configurator_rpc.create_network_function_device_config( - device, config_params) - self._controller.event_complete(event=event, result='SUCCESS') - - def configuration_complete(self, event): - nfp_context = event.context - nf_id = nfp_context['network_function']['id'] - event_results = event.result - for result in event_results: - if result.result.lower() != "success": - device = self._prepare_failure_case_device_data(nfp_context) - self._create_event(event_id='DEVICE_CREATE_FAILED', - event_data=device) - return self._controller.event_complete(event, result="FAILED") - sc_event = self._controller.new_event(id="SERVICE_CONFIGURED", - key=nf_id) - self._controller.post_event(sc_event) - self._controller.event_complete(event, result="SUCCESS") - - def device_configuration_complete(self, event, result='SUCCESS'): - nfp_context = event.data['nfp_context'] - - # Invoke event_complete for original event which is - # CREATE_DEVICE_CONFIGURATION - event_desc = nfp_context.pop('event_desc', None) - key = nfp_context.pop('key', None) - self._controller.event_complete(event) - event = self._controller.new_event(id="CREATE_DEVICE_CONFIGURATION", - key=key, desc_dict=event_desc) - event.binding_key = nfp_context.pop('binding_key', None) - self._controller.event_complete(event, result=result) - - def delete_network_function_device(self, event): - network_function_details = event.context - nfd = network_function_details['network_function_device'] - if not nfd: - self._controller.event_complete(event, result="SUCCESS") - return - device = self._prepare_device_data_fast(network_function_details) - LOG.info("Recieved DELETE NETWORK FUNCTION " - "DEVICE request ") - device['event_desc'] = event.desc.to_dict() - self._create_event(event_id='DELETE_CONFIGURATION', - event_data=device, - is_internal_event=True) - - def delete_device_configuration(self, event): - device = event.data - orchestration_driver = self._get_orchestration_driver( - device['service_details']['service_vendor']) - nfp_context = module_context.get() - config_params = ( - orchestration_driver.get_network_function_device_config( - device, nfp_constants.GENERIC_CONFIG, is_delete=True, - devices_data=nfp_context)) - if not config_params: - self._create_event(event_id='DRIVER_ERROR', - event_data=device, - is_internal_event=True) - nf_id = device['network_function_id'] - dnfd_event = ( - self._controller.new_event( - id='DELETE_NETWORK_FUNCTION_DEVICE', - key=nf_id, - binding_key=nf_id, - desc_dict=device.get('event_desc'))) - self._controller.event_complete(dnfd_event, result='FAILED') - # TODO(mak): If driver returns ERROR, - # then we are not proceeding further - # Stale vms will exist in this case. - # Need to handle this case where - # driver returned None So dont initiate configurator API but call - # unplug_interfaces and device delete to delete vms. - return None - - nf_data = { - 'service_chain_instance': nfp_context.get( - 'service_chain_instance'), - 'provider': nfp_context.get('provider'), - 'consumer': nfp_context.get('consumer') - } - device.update(nf_data) - # Sends RPC call to configurator to delete generic config API - self.configurator_rpc.delete_network_function_device_config( - device, config_params) - - def unplug_interfaces(self, event): - result = "SUCCESS" - device = event.data - self._decrement_device_ref_count(device) - orchestration_driver = self._get_orchestration_driver( - device['service_details']['service_vendor']) - - is_interface_unplugged = ( - orchestration_driver.unplug_network_function_device_interfaces( - device)) - if is_interface_unplugged: - mgmt_port_id = device['mgmt_port_id'] - self._decrement_device_interface_count(device) - device['mgmt_port_id'] = mgmt_port_id - else: - result = "FAILED" - self._create_event(event_id='DELETE_DEVICE', - event_data=device, - is_internal_event=True) - self._controller.event_complete(event, result=result) - - def delete_configuration_complete(self, event): - device = event.data['nfp_context'] - nfd_id = event.data['network_function_device_id'] - nf_id = event.data['network_function_id'] - unplug_interfaces = ( - self._controller.new_event(id='UNPLUG_INTERFACES', - data=device, - key=nf_id, - binding_key=nfd_id, - serialize=True)) - self._controller.post_event(unplug_interfaces) - self._controller.event_complete(event) - - def delete_device(self, event): - # Update status in DB, send DEVICE_DELETED event to NSO. - device = event.data - orchestration_driver = self._get_orchestration_driver( - device['service_details']['service_vendor']) - - network_function = ( - self.nsf_db.get_network_function( - self.db_session, - device['network_function_id'])) - device['network_function'] = network_function - chm_event = self._controller.new_event( - id='PERFORM_CLEAR_HM', - key=device['network_function_id'], - data=device) - self._controller.post_event(chm_event) - - orchestration_driver.delete_network_function_device(device) - self._create_event( - event_id='DEVICE_BEING_DELETED', - event_data=device, - is_poll_event=True, - original_event=event, - max_times=nfp_constants.DEVICE_BEING_DELETED_MAXRETRY) - - @nfp_api.poll_event_desc( - event='DEVICE_BEING_DELETED', - spacing=nfp_constants.DEVICE_BEING_DELETED_SPACING) - def check_device_deleted(self, event): - device = event.data - orchestration_driver = self._get_orchestration_driver( - device['service_details']['service_vendor']) - status = orchestration_driver.get_network_function_device_status( - device, ignore_failure=True) - if not status: - try: - device_id = device['id'] - del device['id'] - orchestration_driver.delete_network_function_device(device) - self._delete_network_function_device_db(device_id, device) - if device.get('event_desc'): - nf_id = device['network_function_id'] - dnfd_event = ( - self._controller.new_event( - id='DELETE_NETWORK_FUNCTION_DEVICE', - key=nf_id, - binding_key=nf_id, - desc_dict=device['event_desc'])) - self._controller.event_complete( - dnfd_event, result='SUCCESS') - return STOP_POLLING - except Exception as exc: - device['id'] = device_id - err = ("Exception - %s - in DEVICE_BEING_DELETED" % (exc)) - LOG.error(err) - return CONTINUE_POLLING - else: - return CONTINUE_POLLING - - # Error Handling - def handle_device_create_error(self, event): - device = event.data - LOG.error("Device creation failed, for device %(device)s", - {'device': device}) - device['network_function_device_id'] = device.get('id') - self._create_event(event_id='DEVICE_CREATE_FAILED', - event_data=device) - - def handle_device_not_up(self, event): - device = event.data - self._release_cnfd_lock(device) - status = nfp_constants.ERROR - desc = 'Device not became ACTIVE' - self._update_network_function_device_db(device, status, desc) - device['network_function_device_id'] = device['id'] - self._create_event(event_id='DEVICE_CREATE_FAILED', - event_data=device) - - def _prepare_failure_case_device_data(self, nfp_context): - network_function = nfp_context['network_function'] - network_function_instance = nfp_context['network_function_instance'] - device = {'network_function_id': network_function['id'], - 'network_function_instance_id': network_function_instance[ - 'id'], 'binding_key': nfp_context.get('binding_key')} - network_function_device = nfp_context.get('network_function_device') - if network_function_device: - device.update( - {'network_function_device_id': network_function_device['id']}) - device.update(network_function_device) - return device - - def handle_plug_interface_failed(self, event): - nfp_context = event.context - device = self._prepare_failure_case_device_data(nfp_context) - # self._release_cnfd_lock(device) - status = nfp_context['network_function_device']['status'] - desc = "Failed to plug interfaces" - self._update_network_function_device_db(device, status, desc) - # self._create_event(event_id='DEVICE_CREATE_FAILED', - # event_data=device) - - def handle_device_not_reachable(self, event): - device = event.data - status = nfp_constants.ERROR - desc = 'Device not reachable, Health Check Failed' - self._update_network_function_device_db(device, status, desc) - device['network_function_device_id'] = device['id'] - # self._create_event(event_id='DEVICE_CREATE_FAILED', - # event_data=device) - self.health_monitor_complete(event, result='FAILED') - - def periodic_hm_handle_device_reachable(self, event): - device = event.data - status = nfp_constants.ACTIVE - desc = 'Device is ACTIVE' - self._update_network_function_device_db(device, status, desc) - - def periodic_hm_handle_device_not_reachable(self, event): - device = event.data - status = nfp_constants.ERROR - desc = 'Device not reachable, Health Check Failed' - self._update_network_function_device_db(device, status, desc) - - def handle_device_config_failed(self, event): - # device = event.data - nfp_context = event.data['nfp_context'] - - device = nfp_context['network_function_device'] - status = device['status'] - desc = 'Configuring Device Failed.' - self._update_network_function_device_db(device, status, desc) - device['network_function_device_id'] = device['id'] - # self._create_event(event_id='DEVICE_CREATE_FAILED', - # event_data=event.data) - LOG.debug("Device create failed for device: %s, with " - "data: %s", (device['id'], device)) - self.device_configuration_complete(event, result='FAILED') - - def handle_interfaces_setup_failed(self, event): - device = event.data - status = nfp_constants.ERROR - desc = 'Interfaces Plugging failed' - self._update_network_function_device_db(device, status, desc) - device['network_function_device_id'] = device['id'] - self._create_event(event_id='DEVICE_CREATE_FAILED', - event_data=device) - LOG.debug("Interface Plugging failed for device: %s," - "with config: %s", (device['id'], device)) - - def handle_driver_error(self, event): - device = event.data - LOG.error("Exception occured in driver, driver returned None " - " for device %(device)s", {'device': device}) - status = nfp_constants.ERROR - desc = 'Exception in driver, driver return None' - self._update_network_function_device_db(device, status, desc) - # device['network_function_device_id'] = device['id'] - # self._create_event(event_id='DEVICE_CREATE_FAILED', - # event_data=device) - - def update_config_params(self, event): - self._create_event(event_id='DEVICE_CONFIG_PARAMETERS_UPDATED', - event_data=event.data, is_internal_event=True) - self._controller.event_complete(event=event, result='SUCCESS') - - -class NDOConfiguratorRpcApi(object): - """Service Manager side of the Service Manager to Service agent RPC API""" - API_VERSION = '1.0' - target = messaging.Target(version=API_VERSION) - - def __init__(self, context, conf): - super(NDOConfiguratorRpcApi, self).__init__() - self.conf = conf - self.context = context - self.client = n_rpc.get_client(self.target) - self.rpc_api = self.client.prepare( - version=self.API_VERSION, - topic=nsf_topics.NFP_NDO_CONFIGURATOR_TOPIC) - - def _get_request_info(self, device, operation): - nfp_context = module_context.get() - request_info = { - 'nf_id': device['network_function_id'], - 'nfi_id': ( - device['network_function_instance_id']), - 'nfd_id': device['id'], - 'requester': nfp_constants.DEVICE_ORCHESTRATOR, - 'operation': operation, - 'logging_context': nfp_context['log_context'], - # So that notification callbacks can work on cached data - # 'orig_nfp_context': device.get('orig_nfp_context'), - 'nfp_context': device.get('nfp_context', None), - 'service_profile': device.get('service_profile'), - 'service_vm_context': nfp_utils.get_service_vm_context( - device['service_details']['service_vendor']), - } - nfd_ip = device.get('mgmt_ip_address') - request_info.update({'device_ip': nfd_ip}) - return request_info - - def _update_params(self, device_data, config_params, operation): - request_info = self._get_request_info(device_data, operation) - if not config_params: - return None - config_params['info'] = { - 'service_type': device_data['service_details']['service_type'], - 'service_vendor': device_data['service_details']['service_vendor'], - 'context': request_info, - } - if device_data.get('service_feature'): - config_params['info'].update( - {'service_feature': device_data.get('service_feature')}) - if config_params.get('service_info'): - config_params['info'].update(config_params.pop('service_info')) - - def create_network_function_device_config(self, device_data, - config_params): - self._update_params(device_data, config_params, operation='create') - LOG.info("Sending create NFD config request to configurator " - "for NF:%(nf_id)s ", - {'nf_id': config_params['info']['context']['nf_id']}) - - transport.send_request_to_configurator(self.conf, - self.context, - config_params, - 'CREATE', - True) - - def delete_network_function_device_config(self, device_data, - config_params): - self._update_params(device_data, config_params, operation='delete') - config_params['info']['context']['nfp_context'] = device_data - LOG.info("Sending delete NFD config request to configurator ") - - transport.send_request_to_configurator(self.conf, - self.context, - config_params, - 'DELETE', - True) - - -class ExceptionHandler(object): - - @staticmethod - def event_method_mapping(event_id): - event_handler_mapping = { - "CREATE_NETWORK_FUNCTION_DEVICE": ( - ExceptionHandler.create_network_function_device), - "DEVICE_SPAWNING": ExceptionHandler.device_spawning, - "PERFORM_INITIAL_HEALTH_CHECK": - ExceptionHandler.perform_initial_health_check, - "DEVICE_UP": ExceptionHandler.device_up, - "PLUG_INTERFACES": ExceptionHandler.plug_interfaces, - "HEALTH_MONITOR_COMPLETE": - ExceptionHandler.health_monitor_complete, - "CREATE_DEVICE_CONFIGURATION": - ExceptionHandler.create_device_configuration, - "DEVICE_CONFIGURED": - ExceptionHandler.device_configuration_complete, - "CONFIGURATION_COMPLETE": ExceptionHandler.configuration_complete, - "DELETE_NETWORK_FUNCTION_DEVICE": ( - ExceptionHandler.delete_network_function_device), - "DELETE_CONFIGURATION": - ExceptionHandler.delete_device_configuration, - "DELETE_CONFIGURATION_COMPLETED": ( - ExceptionHandler.delete_configuration_complete), - "UNPLUG_INTERFACES": ExceptionHandler.unplug_interfaces, - "DELETE_DEVICE": ExceptionHandler.delete_device, - "DEVICE_BEING_DELETED": ExceptionHandler.device_being_deleted, - "PERIODIC_HM_DEVICE_NOT_REACHABLE": ( - ExceptionHandler.periodic_hm_handle_device_not_reachable), - "DEVICE_NOT_REACHABLE": ( - ExceptionHandler.health_monitor_complete), - "DEVICE_CONFIGURATION_FAILED": ( - ExceptionHandler.device_configuration_complete), - "PERFORM_PERIODIC_HEALTH_CHECK": ( - ExceptionHandler.perform_periodic_health_check), - } - if event_id not in event_handler_mapping: - raise Exception(_("Invalid event ID")) - else: - return event_handler_mapping[event_id] - - @staticmethod - def handle(orchestrator, event, exception): - exc_type, exc_value, exc_traceback = sys.exc_info() - message = "Traceback: %s" % traceback.format_exception( - exc_type, exc_value, exc_traceback) - LOG.error(message) - - exception_handler = ExceptionHandler.event_method_mapping(event.id) - return exception_handler(orchestrator, event, exception) - - @staticmethod - def create_network_function_device(orchestrator, event, exception): - nfp_context = event.context - network_function = nfp_context['network_function'] - # [REVISIT: AKASH] Updating NF from device_orchestrator is wrong way - # of doing, but still doing it, will correct it later - orchestrator.nsf_db.update_network_function( - orchestrator.db_session, - network_function['id'], - {'status': nfp_constants.ERROR}) - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def perform_initial_health_check(orchestrator, event, exception): - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def device_up(orchestrator, event, exception): - nfp_context = event.context - network_function = nfp_context['network_function'] - device = orchestrator._prepare_failure_case_device_data(nfp_context) - orchestrator._release_cnfd_lock(device) - orchestrator.nsf_db.update_network_function( - orchestrator.db_session, - network_function['id'], - {'status': nfp_constants.ERROR}) - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def plug_interfaces(orchestrator, event, exception): - nfp_context = event.context - device = orchestrator._prepare_failure_case_device_data(nfp_context) - status = nfp_context['network_function_device']['status'] - desc = "Failed to plug interfaces" - orchestrator._update_network_function_device_db(device, status, desc) - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def health_monitor_complete(orchestrator, event, exception): - nfp_context = event.data['nfp_context'] - event_desc = nfp_context.pop('event_desc', None) - nfp_context.pop('id', None) - key = nfp_context.pop('key', None) - ev = orchestrator._controller.new_event( - id="PERFORM_INITIAL_HEALTH_CHECK", - key=key, desc_dict=event_desc) - orchestrator._controller.event_complete(ev, result='FAILED') - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def create_device_configuration(orchestrator, event, exception): - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def device_configuration_complete(orchestrator, event, exception): - nfp_context = event.data['nfp_context'] - event_desc = nfp_context.pop('event_desc') - key = nfp_context.pop('key') - ev = orchestrator._controller.new_event( - id="CREATE_DEVICE_CONFIGURATION", - key=key, desc_dict=event_desc) - ev.binding_key = nfp_context.pop('binding_key') - orchestrator._controller.event_complete(ev, result='FAILED') - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def configuration_complete(orchestrator, event, exception): - nfp_context = event.context - network_function = nfp_context['network_function'] - orchestrator.nsf_db.update_network_function( - orchestrator.db_session, - network_function['id'], - {'status': nfp_constants.ERROR}) - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def delete_network_function_device(orchestrator, event, exception): - network_function_details = event.context - device = network_function_details['network_function_device'] - status = nfp_constants.ERROR - desc = 'Exception in driver, driver return None' - orchestrator._update_network_function_device_db(device, status, desc) - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def delete_device_configuration(orchestrator, event, exception): - device = event.data - status = nfp_constants.ERROR - desc = 'Exception in driver, driver return None' - orchestrator._update_network_function_device_db(device, status, desc) - nf_id = device['network_function_id'] - dnfd_event = ( - orchestrator._controller.new_event( - id='DELETE_NETWORK_FUNCTION_DEVICE', - key=nf_id, - binding_key=nf_id, - desc_dict=device.get('event_desc'))) - orchestrator._controller.event_complete(dnfd_event, result='FAILED') - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def delete_configuration_complete(orchestrator, event, exception): - device = event.data - status = nfp_constants.ERROR - desc = 'Exception in driver, driver return None' - orchestrator._update_network_function_device_db(device, status, desc) - nf_id = device['network_function_id'] - dnfd_event = ( - orchestrator._controller.new_event( - id='DELETE_NETWORK_FUNCTION_DEVICE', - key=nf_id, - binding_key=nf_id, - desc_dict=device.get('event_desc'))) - orchestrator._controller.event_complete(dnfd_event, result='FAILED') - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def unplug_interfaces(orchestrator, event, exception): - device = event.data - nf_id = device['network_function_id'] - dnfd_event = ( - orchestrator._controller.new_event( - id='DELETE_NETWORK_FUNCTION_DEVICE', - key=nf_id, - binding_key=nf_id, - desc_dict=device.get('event_desc'))) - orchestrator._controller.event_complete(dnfd_event, result='FAILED') - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def delete_device(orchestrator, event, exception): - device = event.data - status = nfp_constants.ERROR - desc = 'Exception in driver, driver return None' - orchestrator._update_network_function_device_db(device, status, desc) - nf_id = device['network_function_id'] - dnfd_event = ( - orchestrator._controller.new_event( - id='DELETE_NETWORK_FUNCTION_DEVICE', - key=nf_id, - binding_key=nf_id, - desc_dict=device.get('event_desc'))) - orchestrator._controller.event_complete(dnfd_event, result='FAILED') - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def device_being_deleted(orchestrator, event, exception): - return {'poll': True} - - @staticmethod - def device_spawning(orchestrator, event, exception): - nfp_context = event.context - network_function = nfp_context['network_function'] - device = orchestrator._prepare_failure_case_device_data(nfp_context) - status = nfp_constants.ERROR - desc = 'Exception in driver, driver return None' - orchestrator._update_network_function_device_db(device, status, desc) - orchestrator._release_cnfd_lock(device) - orchestrator.nsf_db.update_network_function( - orchestrator.db_session, - network_function['id'], - {'status': nfp_constants.ERROR}) - orchestrator._controller.event_complete(event, result='FAILED') - return {'poll': False} - - @staticmethod - def periodic_hm_handle_device_not_reachable(orchestrator, - event, exception): - - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def perform_periodic_health_check(orchestrator, event, exception): - orchestrator._controller.event_complete(event, result='FAILED') diff --git a/gbpservice/nfp/orchestrator/modules/service_orchestrator.py b/gbpservice/nfp/orchestrator/modules/service_orchestrator.py deleted file mode 100644 index 41eaa2bf7..000000000 --- a/gbpservice/nfp/orchestrator/modules/service_orchestrator.py +++ /dev/null @@ -1,2950 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import sys -import traceback - -from neutron_lib import context as n_context -from neutron_lib import rpc as n_rpc -from oslo_log import helpers as log_helpers -import oslo_messaging - -from gbpservice._i18n import _ -from gbpservice.neutron.db import api as db_api -from gbpservice.nfp.common import constants as nfp_constants -from gbpservice.nfp.common import exceptions as nfp_exc -from gbpservice.nfp.common import topics as nfp_rpc_topics -from gbpservice.nfp.core import context as module_context -from gbpservice.nfp.core.event import Event -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.core import module as nfp_api -from gbpservice.nfp.core import path as nfp_path -from gbpservice.nfp.core.rpc import RpcAgent -from gbpservice.nfp.lib import nfp_context_manager as nfp_ctx_mgr -from gbpservice.nfp.lib import transport -from gbpservice.nfp.orchestrator.config_drivers import heat_driver -from gbpservice.nfp.orchestrator.db import nfp_db as nfp_db -from gbpservice.nfp.orchestrator.openstack import openstack_driver - -LOG = nfp_logging.getLogger(__name__) - -STOP_POLLING = {'poll': False} -CONTINUE_POLLING = {'poll': True} -GATEWAY_SERVICES = [nfp_constants.FIREWALL, nfp_constants.VPN] - - -def rpc_init(controller, config): - rpcmgr = RpcHandler(config, controller) - agent = RpcAgent(controller, - host=config.host, - topic=nfp_rpc_topics.NFP_NSO_TOPIC, - manager=rpcmgr) - configurator_rpcmgr = RpcHandlerConfigurator(config, controller) - configurator_agent = RpcAgent( - controller, - host=config.host, - topic=nfp_rpc_topics.NFP_NSO_CONFIGURATOR_TOPIC, - manager=configurator_rpcmgr) - controller.register_rpc_agents([agent, configurator_agent]) - - -def events_init(controller, config, service_orchestrator): - events = ['DELETE_NETWORK_FUNCTION', - 'CREATE_NETWORK_FUNCTION_INSTANCE', - 'DELETE_NETWORK_FUNCTION_INSTANCE', - 'DEVICE_CREATED', - 'DEVICE_ACTIVE', 'DEVICE_DELETED', - 'DEVICE_CREATE_FAILED', 'SEND_USER_CONFIG', - 'CHECK_HEAT_CONFIG_RESULT', 'APPLY_USER_CONFIG', - 'APPLY_USER_CONFIG_BASEMODE', - 'DELETE_USER_CONFIG', 'UPDATE_USER_CONFIG', - 'POLICY_TARGET_ADD', 'POLICY_TARGET_REMOVE', - 'CONSUMER_ADD', 'CONSUMER_REMOVE', - 'APPLY_USER_CONFIG_IN_PROGRESS', - 'INITIATE_USER_CONFIG', - 'UPDATE_NETWORK_FUNCTION_DESCRIPTION', - 'UPDATE_USER_CONFIG_PREPARING_TO_START', - 'UPDATE_USER_CONFIG_IN_PROGRESS', - 'UPDATE_USER_CONFIG_STILL_IN_PROGRESS', - 'DELETE_USER_CONFIG_IN_PROGRESS', - 'CONFIG_APPLIED', 'USER_CONFIG_APPLIED', 'USER_CONFIG_DELETED', - 'USER_CONFIG_DELETE_FAILED', 'USER_CONFIG_UPDATE_FAILED', - 'USER_CONFIG_FAILED', 'CHECK_USER_CONFIG_COMPLETE', - 'SERVICE_CONFIGURED', 'CREATE_NETWORK_FUNCTION_INSTANCE_DB', - 'DELETE_NETWORK_FUNCTION_DB'] - events_to_register = [] - for event in events: - events_to_register.append( - Event(id=event, handler=service_orchestrator)) - controller.register_events(events_to_register) - - -def nfp_module_init(controller, config): - events_init(controller, config, ServiceOrchestrator(controller, config)) - rpc_init(controller, config) - - -class RpcHandler(object): - - """RPC Handler for Node Driver to NFP. - - Network Function methods are invoked in an RPC Call by the - node driver and data has to be returned by the orchestrator. - """ - - RPC_API_VERSION = '1.0' - target = oslo_messaging.Target(version=RPC_API_VERSION) - - def __init__(self, conf, controller): - super(RpcHandler, self).__init__() - self.conf = conf - self._controller = controller - # REVISIT (mak): Can a ServiceOrchestrator object be - # initialized here and used for each rpc ? - - @log_helpers.log_method_call - def create_network_function(self, context, network_function): - '''Create Network Function. - - Invoked in an RPC Call. Return the Network function DB object - created. Results in an Event for async processing of Network - Function Instance - ''' - module_context.init(network_function) - LOG.info("Received RPC call for CREATE NETWORK FUNCTION for " - "tenant:%(tenant_id)s", - {'tenant_id': network_function[ - 'resource_owner_context']['tenant_id']}) - - service_orchestrator = ServiceOrchestrator(self._controller, self.conf) - return service_orchestrator.create_network_function( - context, network_function) - - @log_helpers.log_method_call - def get_network_function(self, context, network_function_id): - '''Invoked in an RPC Call. Return the Network function DB object''' - module_context.init() - LOG.debug("Received RPC call for GET NETWORK FUNCTION for NFI %s", - network_function_id) - - service_orchestrator = ServiceOrchestrator(self._controller, self.conf) - return service_orchestrator.get_network_function( - context, network_function_id) - - @log_helpers.log_method_call - def get_network_functions(self, context, filters=None): - '''Invoked in an RPC Call. - - Returns the Network functions from DB - ''' - module_context.init() - LOG.info("Received RPC call for GET NETWORK FUNCTIONS ") - service_orchestrator = ServiceOrchestrator(self._controller, self.conf) - return service_orchestrator.get_network_functions( - context, filters) - - @log_helpers.log_method_call - def update_network_function(self, context, network_function_id, - config): - '''Update Network Function Configuration. - - Invoked in an RPC call. Return the updated Network function DB object. - Results in an Event for async processing of Network Function Instance. - - ''' - module_context.init() - LOG.info("Received RPC call for UPDATE NETWORK FUNCTION for NF:" - "%(network_function_id)s", - {'network_function_id': network_function_id}) - service_orchestrator = ServiceOrchestrator(self._controller, self.conf) - service_orchestrator.update_network_function( - context, network_function_id, config) - - @log_helpers.log_method_call - def delete_network_function(self, context, network_function_id, - network_function_data): - '''Delete the network Function. - - Invoked in an RPC call. Return the updated Network function DB object. - Results in an Event for async processing of Network Function Instance. - ''' - module_context.init() - LOG.info("Received RPC call for DELETE NETWORK FUNCTION for NF:" - "%(network_function_id)s", - {'network_function_id': network_function_id}) - - service_orchestrator = ServiceOrchestrator(self._controller, self.conf) - service_orchestrator.delete_network_function( - context, network_function_id, network_function_data) - - @log_helpers.log_method_call - def policy_target_added_notification(self, context, network_function_id, - policy_target): - '''Update Configuration to react to member addition. - - Invoked in an RPC call. Return the updated Network function DB object. - Results in an Event for async processing of Network Function Instance. - ''' - module_context.init() - LOG.info("Received RPC call for POLICY TARGET ADDED NOTIFICATION " - "for NF:" - " %(network_function_id)s", - {'network_function_id': network_function_id}) - service_orchestrator = ServiceOrchestrator(self._controller, self.conf) - service_orchestrator.handle_policy_target_added( - context, network_function_id, policy_target) - - @log_helpers.log_method_call - def policy_target_removed_notification(self, context, network_function_id, - policy_target): - '''Update Configuration to react to member deletion. - - Invoked in an RPC call. Return the updated Network function DB object. - Results in an Event for async processing of Network Function Instance. - ''' - module_context.init() - LOG.info("Received RPC call for POLICY TARGET REMOVED " - "NOTIFICATION for NF:%(network_function_id)s", - {'network_function_id': network_function_id}) - service_orchestrator = ServiceOrchestrator(self._controller, self.conf) - service_orchestrator.handle_policy_target_removed( - context, network_function_id, policy_target) - - @log_helpers.log_method_call - def consumer_ptg_added_notification(self, context, network_function_id, - policy_target_group): - '''Update Configuration to react to consumer PTG creation. - - Invoked in an RPC call. Return the updated Network function DB object. - Results in an Event for async processing of Network Function Instance. - ''' - module_context.init() - LOG.info("Received RPC call CONSUMER PTG ADDED NOTIFICATION " - "for NF:%(network_function_id)s", - {'network_function_id': network_function_id}) - service_orchestrator = ServiceOrchestrator(self._controller, self.conf) - service_orchestrator.handle_consumer_ptg_added( - context, network_function_id, policy_target_group) - - @log_helpers.log_method_call - def consumer_ptg_removed_notification(self, context, network_function_id, - policy_target_group): - '''Update Configuration to react to consumer PTG deletion. - - Invoked in an RPC call. Return the updated Network function DB object. - Results in an Event for async processing of Network Function Instance. - ''' - module_context.init() - LOG.info("Received RPC call for CONSUMER PTG REMOVED NOTIFICATION " - "for NF:%(network_function_id)s", - {'network_function_id': network_function_id}) - service_orchestrator = ServiceOrchestrator(self._controller, self.conf) - service_orchestrator.handle_consumer_ptg_removed( - context, network_function_id, policy_target_group) - - @log_helpers.log_method_call - def get_network_function_details(self, context, network_function_id): - '''Invoked in an RPC Call. - - Return the Network function Details object - ''' - module_context.init() - LOG.debug("Received RPC call for GET NETWORK FUNCTION DETAILS in " - "for NF:%s", - network_function_id) - service_orchestrator = ServiceOrchestrator(self._controller, self.conf) - return service_orchestrator.get_network_function_details( - network_function_id) - - @log_helpers.log_method_call - def get_port_info(self, context, port_id): - '''Invoked in an RPC Call. Return the Port Info Details object''' - module_context.init() - LOG.debug("Received RPC call for GET PORT INFO in " - "for PORT ID:%s", - port_id) - service_orchestrator = ServiceOrchestrator(self._controller, self.conf) - return service_orchestrator.get_port_info(port_id) - - @log_helpers.log_method_call - def get_network_function_context(self, context, network_function_id): - '''Invoked in an RPC Call. - Return the Network function context - ''' - module_context.init() - LOG.debug("Received RPC call for GET NETWORK FUNCTION CONTEXT in " - "for NF:%s", - network_function_id) - service_orchestrator = ServiceOrchestrator(self._controller, self.conf) - return service_orchestrator.get_network_function_context( - network_function_id) - - @log_helpers.log_method_call - def get_plumbing_info(self, context, request_info): - module_context.init() - LOG.debug("Received RPC call for GET PLUMBING INFO " - "for request info:%s", - request_info) - service_orchestrator = ServiceOrchestrator(self._controller, self.conf) - return service_orchestrator.get_pt_info_for_plumbing(request_info) - - -class RpcHandlerConfigurator(object): - - """RPC Handler for notificationrpcs from - Configurator to orchestrator. - """ - - RPC_API_VERSION = '1.0' - target = oslo_messaging.Target(version=RPC_API_VERSION) - - def __init__(self, conf, controller): - super(RpcHandlerConfigurator, self).__init__() - self.conf = conf - self._controller = controller - self.rpc_event_mapping = { - 'heat': ['CHECK_HEAT_CONFIG_RESULT', - 'DELETE_USER_CONFIG', - 'UPDATE_USER_CONFIG', - 'POLICY_TARGET_ADD', - 'POLICY_TARGET_REMOVE', - 'CONSUMER_ADD', - 'CONSUMER_REMOVE'] - } - - def _log_event_created(self, event_id, event_data): - network_function_instance = event_data.get('network_function_instance') - if network_function_instance: - NF = network_function_instance.get('network_function_id') - NFI = network_function_instance.get('id') - else: - NF = None - NFI = None - if NF and NFI: - LOG.info("Created event %(event_name)s with NF:%(nf)s " - "and NFI:%(nfi)s ", - {'event_name': event_id, - 'nf': NF, - 'nfi': NFI}) - else: - LOG.info("Created event %(event_name)s ", - {'event_name': event_id}) - - def _create_event(self, event_id, event_data=None, - is_poll_event=False, original_event=None, - serialize=False, max_times=10): - if is_poll_event: - ev = self._controller.new_event( - id=event_id, data=event_data, - serialize=original_event.sequence, - binding_key=original_event.binding_key, - key=original_event.key) - LOG.debug("poll event started for %s", (ev.id)) - self._controller.poll_event(ev, max_times=10) - else: - if serialize: - network_function_id = event_data['network_function_id'] - ev = self._controller.new_event( - id=event_id, data=event_data, - binding_key=network_function_id, - key=network_function_id, - serialize=True) - else: - ev = self._controller.new_event( - id=event_id, - data=event_data) - self._controller.post_event(ev) - self._log_event_created(event_id, event_data) - - @log_helpers.log_method_call - def network_function_notification(self, context, notification_data): - nfp_context = module_context.init() - info = notification_data.get('info') - responses = notification_data.get('notification') - request_info = info.get('context') - operation = request_info.get('operation') - logging_context = request_info.get('logging_context', {}) - nfp_context['log_context'] = logging_context - if 'nfp_context' in request_info: - nfp_context.update(request_info['nfp_context']) - - serialize = False - - for response in responses: - resource = response.get('resource') - data = response.get('data') - result = data.get('status_code') - - if result.lower() != 'success': - if operation == 'create': - event_id = self.rpc_event_mapping[resource][0] - elif operation == 'delete': - # No need to handle this - # event_id = self.rpc_event_mapping[resource][1] - return - elif operation == 'update': - serialize = True - event_id = self.rpc_event_mapping[resource][2] - elif operation == 'pt_add': - serialize = True - event_id = self.rpc_event_mapping[resource][3] - elif operation == 'pt_remove': - serialize = True - event_id = self.rpc_event_mapping[resource][4] - elif operation == 'consumer_add': - serialize = True - event_id = self.rpc_event_mapping[resource][5] - else: - serialize = True - event_id = self.rpc_event_mapping[resource][6] - break - else: - if operation == 'delete': - event_id = 'USER_CONFIG_DELETED' - else: - event_id = 'CONFIG_APPLIED' - nf_id = request_info.pop('nf_id') - nfi_id = request_info.pop('nfi_id') - nfd_id = request_info.pop('nfd_id') - request_info['network_function_id'] = nf_id - request_info['network_function_instance_id'] = nfi_id - request_info['network_function_device_id'] = nfd_id - event_data = request_info - self._create_event(event_id=event_id, - event_data=event_data, serialize=serialize) - - -class NFPDbPatch(nfp_db.NFPDbBase): - - """Patch for Db class. - - This class is used by service orchestrator to complete the path. - In the CREATE/UPDATE operations, at last service orchestrator - invokes update_network_function to update status to be ACTIVE/ERROR, - and there are many error paths. Instead of doing 'path_complete' - at multiple places, patched the Db class to override update & - delete network_function methods. Here, the path is completed and - then the base class methods are invoked to do the actual db operation. - """ - - def __init__(self, controller): - self._controller = controller - super(NFPDbPatch, self).__init__() - - def update_network_function(self, session, network_function_id, - updated_network_function): - status = updated_network_function.get('status') - if status == 'ACTIVE' or status == 'ERROR': - self._controller.path_complete_event() - return super(NFPDbPatch, self).update_network_function( - session, network_function_id, updated_network_function) - - def delete_network_function(self, session, network_function_id): - self._controller.path_complete_event() - return super(NFPDbPatch, self).delete_network_function( - session, network_function_id) - - -class ServiceOrchestrator(nfp_api.NfpEventHandler): - - """Orchestrator For Network Services - - This class handles the orchestration of Network Function lifecycle. - It deals with logical service resources - Network Functions and Network - Function Instances. There is a one-to-many mapping between Network - Functions and Network Function instances. For eg. a Network Function in - HA mode might have two Network Function Instances - Active, Standby - whereas a Network Function in Cluster mode might have more than 2 Network - Function Instances. This module interacts with Device Orchestrator and - Config driver. - - Workflow for create: - 1) create_network_function is called in the context of an RPC call. This - method generates an event CREATE_NETWORK_FUNCTION_INSTANCE - 2) Event handler for CREATE_NETWORK_FUNCTION_INSTANCE. Here a DB entry is - created and generates an event CREATE_NETWORK_FUNCTION_DEVICE. - 3) The Device Orchestrator module handles this event and generates an event - DEVICE_CREATED or DEVICE_CREATE_FAILED - 4) Event handler for DEVICE_CREATED event updates the Network Function - Instance DB object with the created Network Function Device ID - 5) Event handler for DEVICE_CREATE_FAILED event updates the Network - Function Instance and Network Function DB with status ERROR - 6) Device orchestrator could then generate DEVICE_ACTIVE or - DEVICE_CREATE_FAILED based on the device being healthy or it being not - reachable - 7) Event handler for DEVICE_ACTIVE updates Network Function Instance to - Active, invokes config driver (heat) to apply user provided service config. - A poll event APPLY_USER_CONFIG_IN_PROGRESS is then created. - 8) Event handler for poll event APPLY_USER_CONFIG_IN_PROGRESS checks - whether the configuration is applied successfully - 9) If the config driver returns COMPLETED or ERROR, the poll event is - stopped and the Network Function is updated to Active or Error. If it - returns IN_PROGRESS, the poll event is continued. - """ - - # REVISIT(ashu): Split this into multiple manageable classes - def __init__(self, controller, config): - self._controller = controller - self.conf = config - self.db_handler = NFPDbPatch(controller) - self.gbpclient = openstack_driver.GBPClient(config) - self.keystoneclient = openstack_driver.KeystoneClient(config) - self.config_driver = heat_driver.HeatDriver(config) - neutron_context = n_context.get_admin_context() - self.configurator_rpc = NSOConfiguratorRpcApi(neutron_context, config) - self.UPDATE_USER_CONFIG_MAXRETRY = ( - nfp_constants.UPDATE_USER_CONFIG_PREPARING_TO_START_MAXRETRY) - self.UPDATE_USER_CONFIG_STILL_IN_PROGRESS_MAXRETRY = ( - nfp_constants.UPDATE_USER_CONFIG_STILL_IN_PROGRESS_MAXRETRY) - self.status_map = { - 'pt_add': {'status': 'PT_ADD_IN_PROGRESS', - 'status_description': 'pt addition is in progress'}, - 'pt_remove': {'status': 'PT_REMOVE_IN_PROGRESS', - 'status_description': 'pt deletion is in progress'}, - 'ptg_add': {'status': 'PTG_ADD_IN_PROGRESS', - 'status_description': 'ptg addition is in progress'}, - 'ptg_remove': {'status': 'PTG_REMOVE_IN_PROGRESS', - 'status_description': ( - 'ptg deletion is in progress')}, - } - - @property - def db_session(self): - return db_api.get_writer_session() - - def event_method_mapping(self, event_id): - event_handler_mapping = { - "DELETE_NETWORK_FUNCTION": self.delete_network_function, - "CREATE_NETWORK_FUNCTION_INSTANCE": ( - self.create_network_function_instance), - "DELETE_NETWORK_FUNCTION_INSTANCE": ( - self.delete_network_function_instance), - "DEVICE_CREATED": self.handle_device_created, - "DEVICE_ACTIVE": self.handle_device_active, - "SEND_USER_CONFIG": self.send_user_config, - "DEVICE_DELETED": self.handle_device_deleted, - "DEVICE_CREATE_FAILED": self.handle_device_create_failed, - "APPLY_USER_CONFIG": self.apply_user_config, - "APPLY_USER_CONFIG_BASEMODE": self.apply_user_config_basemode, - "CHECK_HEAT_CONFIG_RESULT": self.check_heat_config_result, - "DELETE_USER_CONFIG": self.delete_user_config, - "UPDATE_USER_CONFIG": self.handle_update_user_config, - "POLICY_TARGET_ADD": self.policy_target_add_user_config, - "POLICY_TARGET_REMOVE": self.policy_target_remove_user_config, - "CONSUMER_ADD": self.consumer_ptg_add_user_config, - "CONSUMER_REMOVE": self.consumer_ptg_remove_user_config, - "INITIATE_USER_CONFIG": self.initiate_user_config, - "UPDATE_NETWORK_FUNCTION_DESCRIPTION": ( - self.update_network_function_description), - "APPLY_USER_CONFIG_IN_PROGRESS": ( - self.apply_user_config_in_progress), - "CHECK_USER_CONFIG_COMPLETE": ( - self.check_for_user_config_complete), - "UPDATE_USER_CONFIG_PREPARING_TO_START": ( - self.check_for_user_config_deleted), - "UPDATE_USER_CONFIG_IN_PROGRESS": ( - self.handle_continue_update_user_config), - "UPDATE_USER_CONFIG_STILL_IN_PROGRESS": ( - self.apply_user_config_in_progress), - "DELETE_USER_CONFIG_IN_PROGRESS": ( - self.check_for_user_config_deleted_fast), - "CONFIG_APPLIED": self.handle_config_applied, - "USER_CONFIG_APPLIED": self.handle_user_config_applied, - "USER_CONFIG_DELETED": self.handle_user_config_deleted, - "USER_CONFIG_DELETE_FAILED": self.handle_user_config_delete_failed, - "USER_CONFIG_UPDATE_FAILED": self.handle_update_user_config_failed, - "USER_CONFIG_FAILED": self.handle_user_config_failed, - "SERVICE_CONFIGURED": self.handle_service_configured, - "CREATE_NETWORK_FUNCTION_INSTANCE_DB": ( - self.create_network_function_instance_db), - "DELETE_NETWORK_FUNCTION_DB": self.delete_network_function_db - } - if event_id not in event_handler_mapping: - raise Exception(_("Invalid Event ID")) - else: - return event_handler_mapping[event_id] - - def handle_event(self, event): - event_data = event.context - network_function_instance = event_data.get( - 'network_function_instance') - if network_function_instance: - NF = network_function_instance.get('network_function_id') - NFI = network_function_instance.get('id') - else: - NF = None - NFI = None - if NF and NFI: - LOG.info("Received event %(event_name)s with NF:%(nf)s and " - "NFI:%(nfi)s ", - {'event_name': event.id, - 'nf': NF, - 'nfi': NFI}) - else: - LOG.info("Received event %(event_name)s ", - {'event_name': event.id}) - try: - event_handler = self.event_method_mapping(event.id) - event_handler(event) - except Exception as e: - LOG.exception("Error in processing event: %(event_id)s for " - "event data %(event_data)s. Error: %(error)s", - {'event_id': event.id, 'event_data': event.data, - 'error': e}) - _, _, tb = sys.exc_info() - traceback.print_tb(tb) - raise e - - def handle_poll_event(self, event): - LOG.info("Received poll event %(id)s", - {'id': event.id}) - try: - event_handler = self.event_method_mapping(event.id) - return event_handler(event) - except Exception: - LOG.exception("Error in processing poll event: " - "%(event_id)s", {'event_id': event.id}) - - def event_cancelled(self, event, reason): - nfp_context = event.context - if event.id == 'CHECK_USER_CONFIG_COMPLETE': - network_function = nfp_context['network_function'] - LOG.info("Applying user config failed for " - "NF:%(network_function_id)s " - "with reason %(reason)s" - " ", {'network_function_id': network_function[ - 'id'], 'reason': str(reason)}) - operation = nfp_context['log_context'].get('path') - LOG.error("[Event:Service%(operation)sFailed]", - {'operation': operation.capitalize()}) - LOG.event('%s network function failed.' % operation.capitalize(), - stats_type=nfp_constants.error_event) - - binding_key = nfp_context['service_details'][ - 'service_vendor'].lower() + network_function['id'] - # Complete the original event INITIATE_USER_CONFIG here - event_desc = nfp_context.pop('event_desc', None) - apply_config_event = self._controller.new_event( - id='INITIATE_USER_CONFIG', - key=network_function['id'], - desc_dict=event_desc) - apply_config_event.binding_key = binding_key - self._controller.event_complete( - apply_config_event, result="FAILED") - - elif event.id == 'APPLY_USER_CONFIG_IN_PROGRESS' or ( - event.id == 'UPDATE_USER_CONFIG_STILL_IN_PROGRESS'): - request_data = event.data - LOG.info("Applying user config failed for " - "NF: %(network_function_id)s data:" - "%(data)s with reason %(reason)s" - "", {'data': request_data, - 'network_function_id': request_data[ - 'network_function_id'], - 'reason': str(reason)}) - - updated_network_function = {'status': nfp_constants.ERROR} - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock( - self.db_session, - self.db_handler.update_network_function, - request_data['network_function_id'], - updated_network_function) - - operation = nfp_context['log_context'].get('path') - LOG.error("[Event:Service%(operation)sFailed]", - {'operation': operation.capitalize()}) - LOG.event('%s network function failed.' % operation.capitalize(), - stats_type=nfp_constants.error_event) - - elif event.id == 'DELETE_USER_CONFIG_IN_PROGRESS' or ( - event.id == 'UPDATE_USER_CONFIG_PREPARING_TO_START'): - request_data = event.data - event_data = { - 'network_function_id': request_data['network_function_id'] - } - - if event.id == 'DELETE_USER_CONFIG_IN_PROGRESS': - ducf_event = self._controller.new_event( - id='DELETE_USER_CONFIG', - key=request_data['network_function_id'], - binding_key=request_data['network_function_id'], - desc_dict=request_data['event_desc']) - self._controller.event_complete(ducf_event, result="FAILED") - - self._create_event('USER_CONFIG_DELETE_FAILED', - event_data=event_data, is_internal_event=True) - - def handle_exception(self, event, exception): - return ExceptionHandler.handle(self, event, exception) - - def _log_event_created(self, event_id, event_data): - network_function_instance = event_data.get( - 'network_function_instance') - if network_function_instance: - NF = network_function_instance.get('network_function_id') - NFI = network_function_instance.get('id') - else: - NF = None - NFI = None - if NF and NFI: - LOG.info("Created event %(event_name)s with NF:%(nf)s and " - "NFI:%(nfi)s ", - {'event_name': event_id, - 'nf': NF, - 'nfi': NFI}) - else: - LOG.info("Created event %(event_name)s ", - {'event_name': event_id}) - # REVISIT(ashu): Merge this _create_event, and above one to have - # single function. - - def _create_event(self, event_id, event_data=None, - key=None, binding_key=None, serialize=False, - is_poll_event=False, original_event=None, - is_internal_event=False, max_times=20): - if not is_internal_event: - if is_poll_event: - ev = self._controller.new_event( - id=event_id, data=event_data, - serialize=original_event.sequence, - binding_key=original_event.binding_key, - key=original_event.desc.uuid) - LOG.debug("poll event started for %s", (ev.id)) - self._controller.poll_event(ev, max_times=max_times) - else: - if original_event: - ev = self._controller.new_event( - id=event_id, data=event_data, - serialize=original_event.sequence, - binding_key=original_event.binding_key, - key=original_event.desc.uuid) - else: - ev = self._controller.new_event( - id=event_id, - data=event_data) - self._controller.post_event(ev) - nfp_context = module_context.get() - self._log_event_created(event_id, nfp_context) - else: - nfp_context = module_context.get() - if original_event: - event = self._controller.new_event( - id=event_id, data=event_data, - serialize=original_event.sequence, - binding_key=original_event.binding_key, - key=original_event.desc.uuid, - context=nfp_context) - else: - # Same module API, so calling corresponding function - # directly. - event = self._controller.new_event( - id=event_id, - data=event_data, - context=nfp_context) - self.handle_event(event) - - def _get_base_mode_support(self, service_profile_id): - with nfp_ctx_mgr.KeystoneContextManager as kcm: - admin_token = kcm.retry(self.keystoneclient.get_admin_token) - with nfp_ctx_mgr.GBPContextManager as gcm: - service_profile = gcm.retry(self.gbpclient.get_service_profile, - admin_token, service_profile_id) - service_details = transport.parse_service_flavor_string( - service_profile['service_flavor']) - resource_data = {'admin_token': admin_token, - 'service_profile': service_profile, - 'service_details': service_details} - base_mode_support = (True if service_details['device_type'] == 'None' - else False) - return base_mode_support, resource_data - - def _get_service_type(self, service_profile_id): - with nfp_ctx_mgr.KeystoneContextManager as kcm: - admin_token = kcm.retry(self.keystoneclient.get_admin_token) - with nfp_ctx_mgr.GBPContextManager as gcm: - service_profile = gcm.retry(self.gbpclient.get_service_profile, - admin_token, service_profile_id) - service_type = service_profile['service_type'] - return service_type - - def update_network_function_user_config(self, network_function_id, - service_config_str, - operation): - tag_str, config_str = self.config_driver.parse_template_config_string( - service_config_str) - if not config_str: - LOG.error('Exception while parsing config string, config ' - 'string: %(config_str)s is improper for ' - 'network_function id: %(network_function_id)s', - {'config_str': service_config_str, - 'network_function_id': network_function_id}) - self.handle_driver_error(network_function_id) - return None - - if tag_str != nfp_constants.CONFIG_INIT_TAG: - network_function_details = self.get_network_function_details( - network_function_id) - service_type = network_function_details.pop('service_type') - if not service_type: - service_type = self._get_service_type( - network_function_details['network_function'][ - 'service_profile_id']) - network_function_data = { - 'network_function_details': network_function_details, - 'service_type': service_type - } - - rpc_method = getattr(self.configurator_rpc, operation + - '_network_function_user_config') - rpc_method(network_function_data, service_config_str, tag_str) - else: - # Place holder for calling config_init API - pass - - def update_consumer_ptg(self, network_function_data, - service_config_str, operation): - tag_str, config_str = self.config_driver.parse_template_config_string( - service_config_str) - network_function_id = network_function_data[ - 'network_function_details']['network_function']['id'] - if not config_str: - LOG.error('Exception while parsing config string, config ' - 'string: %(config_str)s is improper for ' - 'network_function id: %(network_function_id)s', - {'config_str': service_config_str, - 'network_function_id': network_function_id}) - self.handle_driver_error(network_function_id) - return None - - if tag_str != nfp_constants.CONFIG_INIT_TAG: - rpc_method = getattr(self.configurator_rpc, operation + - '_user_config') - rpc_method(network_function_data, service_config_str, tag_str) - else: - # Place holder for calling config_init API - pass - - def create_network_function_user_config(self, network_function_id, - service_config_str): - self.update_network_function_user_config(network_function_id, - service_config_str, - operation='create') - - def delete_network_function_user_config(self, network_function_id, - service_config_str): - self.update_network_function_user_config(network_function_id, - service_config_str, - operation='delete') - - def consumer_add_user_config(self, network_function_data, - service_config_str): - self.update_consumer_ptg(network_function_data, - service_config_str, - operation='consumer_add') - - def consumer_remove_user_config(self, network_function_data, - service_config_str): - self.update_consumer_ptg(network_function_data, - service_config_str, - operation='consumer_remove') - - def pt_add_user_config(self, network_function_data, - service_config_str): - self.update_consumer_ptg(network_function_data, - service_config_str, - operation='policy_target_add') - - def pt_remove_user_config(self, network_function_data, - service_config): - self.update_consumer_ptg(network_function_data, - service_config, - operation='policy_target_remove') - - def _report_logging_info(self, nf, nfi, service_type, - service_vendor): - LOG.info("[TenantID:%(tenant_id)s, " - "ServiceChainID:%(service_chain_id)s, " - "ServiceInstanceID:%(service_instance_id)s, " - "ServiceType:%(service_type)s, " - "ServiceProvider:%(service_provider)s]", - {'tenant_id': nf['tenant_id'], - 'service_chain_id': nf['service_chain_id'], - 'service_instance_id': nfi['id'], - 'service_type': service_type, - 'service_provider': service_vendor}) - - def _validate_service_vendor(self, service_vendor): - if (service_vendor not in self.conf.orchestrator.supported_vendors): - raise Exception( - _("The NFP Node driver does not support this service " - "profile with the service vendor %s.") % service_vendor) - - def create_network_function(self, context, network_function_info): - self._validate_create_service_input(context, network_function_info) - nfp_context = module_context.get() - service_profile = network_function_info['service_profile'] - service_details = transport.parse_service_flavor_string( - service_profile['service_flavor']) - - with nfp_ctx_mgr.KeystoneContextManager as kcm: - admin_token = kcm.retry(self.keystoneclient.get_admin_token) - admin_tenant_id = kcm.retry( - self.keystoneclient.get_admin_tenant_id, admin_token) - - network_function_info['resource_owner_context'][ - 'admin_token'] = admin_token - network_function_info['resource_owner_context'][ - 'admin_tenant_id'] = admin_tenant_id - - tenant_id = network_function_info['tenant_id'] - - # GBP or Neutron - # mode = network_function_info['network_function_mode'] - service_profile_id = service_profile['id'] - service_id = network_function_info['service_chain_node']['id'] - service_chain_id = network_function_info[ - 'service_chain_instance']['id'] - - base_mode_support = (True if service_details['device_type'] == 'None' - else False) - # REVISIT(ashu): take the first few characters just like neutron does - # with ovs interfaces inside the name spaces.. - name = "%s_%s" % (network_function_info[ - 'service_chain_node']['name'][:6], - network_function_info[ - 'service_chain_instance']['name'][:6]) - service_config_str = network_function_info.pop('service_config') - network_function = { - 'name': name, - 'description': '', - 'tenant_id': tenant_id, - 'service_id': service_id, # GBP Service Node or Neutron Service ID - 'service_chain_id': service_chain_id, # GBP SC instance ID - 'service_profile_id': service_profile_id, - 'service_config': service_config_str, - 'status': nfp_constants.PENDING_CREATE - } - with nfp_ctx_mgr.DbContextManager: - network_function = self.db_handler.create_network_function( - self.db_session, network_function) - network_function.pop('service_config') - - # Update ncp_node_instance_nf_mapping with nf_id - network_function_map = { - 'network_function_id': network_function['id'], - 'status': nfp_constants.PENDING_CREATE, - 'status_details': 'Processing create in orchestrator' - } - with nfp_ctx_mgr.DbContextManager: - self.db_handler.update_node_instance_network_function_map( - self.db_session, service_id, service_chain_id, - network_function_map) - nfp_path.create_path(network_function['id']) - nfp_context['event_desc']['path_type'] = 'create' - nfp_context['event_desc']['path_key'] = network_function['id'] - nfp_context['log_context']['path'] = 'create' - nfp_context['log_context']['meta_id'] = network_function['id'] - nfp_context['log_context']['auth_token'] = context.auth_token - - LOG.info("[Event:ServiceCreateInitiated]") - LOG.event("Started create network function.", - stats_type=nfp_constants.request_event) - - nfp_context.update(network_function_info) - - service_details['service_type'] = service_profile['service_type'] - service_details['network_mode'] = nfp_context['network_function_mode'] - nfp_context['network_function'] = network_function - nfp_context['service_details'] = service_details - nfp_context['share_existing_device'] = False - nfp_context['base_mode'] = base_mode_support - LOG.info("Handling RPC call CREATE NETWORK FUNCTION for " - "%(service_type)s with tenant:%(tenant_id)s", - {'tenant_id': tenant_id, - 'service_type': service_profile['service_type']}) - if base_mode_support: - # Store the context in current thread - # In base mode support, create user config directly, no need to - # create network function instance, network function device first. - self.create_network_function_user_config(network_function['id'], - service_config_str) - else: - # Create and event to perform Network service instance - ev = self._controller.new_event( - id='CREATE_NETWORK_FUNCTION_INSTANCE_DB', - key=network_function['id']) - self._controller.post_event(ev) - # self.create_network_function_instance_db(nfp_context) - - return network_function - - def update_network_function(self, context, network_function_id, - user_config): - nfp_context = module_context.get() - nfp_path.update_path(network_function_id) - nfp_context['event_desc']['path_type'] = 'update' - nfp_context['event_desc']['path_key'] = network_function_id - nfp_context['log_context']['path'] = 'update' - nfp_context['log_context']['meta_id'] = network_function_id - nfp_context['log_context']['auth_token'] = context.auth_token - - # Handle config update - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock(self.db_session, self.db_handler.update_network_function, - network_function_id, - {'service_config': user_config, - 'status': nfp_constants.PENDING_UPDATE}) - LOG.info("[Event:ServiceUpdateInitiated]") - LOG.event("Started update network function.", - stats_type=nfp_constants.request_event) - - self.update_network_function_user_config(network_function_id, - user_config, - operation='update') - - def delete_network_function(self, context, network_function_id, - network_function_data): - nfp_context = module_context.get() - nfp_path.delete_path(network_function_id) - nfp_context['event_desc']['path_type'] = 'delete' - nfp_context['event_desc']['path_key'] = network_function_id - nfp_context['log_context']['path'] = 'delete' - nfp_context['log_context']['meta_id'] = network_function_id - nfp_context['log_context']['auth_token'] = context.auth_token - - network_function_details = self.get_network_function_details( - network_function_id) - service_config = ( - network_function_details['network_function'].pop( - 'service_config')) - service_profile_id = network_function_details[ - 'network_function']['service_profile_id'] - base_mode_support, resource_data = ( - self._get_base_mode_support(service_profile_id)) - with nfp_ctx_mgr.KeystoneContextManager as kcm: - admin_tenant_id = kcm.retry( - self.keystoneclient.get_admin_tenant_id, - resource_data['admin_token']) - network_function_details['admin_tenant_id'] = admin_tenant_id - nfi = network_function_details.get('network_function_instance', None) - nfd = network_function_details.get('network_function_device', None) - nfi_id = nfi.get('id', '-') if nfi else '-' - nfd_id = nfd.get('id', '-') if nfd else '-' - nfp_context['log_context']['nfi_id'] = nfi_id - nfp_context['log_context']['nfd_id'] = nfd_id - - if (not base_mode_support and - not network_function_details[ - 'network_function']['network_function_instances']): - with nfp_ctx_mgr.DbContextManager: - self.db_handler.delete_network_function( - self.db_session, network_function_id) - LOG.info("[Event:ServiceDeleteCompleted]") - LOG.event("Completed delete network function.", - stats_type=nfp_constants.response_event) - - # network_function_details['service_type is None because - # nfp core context is not set - # so getting service_type from resource_data - service_type = resource_data['service_profile']['service_type'] - LOG.event("Sending service deleted event to controller.", - type='SERVICE_DELETED', - nf_id=network_function_id, - service_type=service_type) - return - network_function_details.update(resource_data) - network_function_details.update( - {'base_mode_support': base_mode_support}) - network_function = { - 'status': nfp_constants.PENDING_DELETE - } - service_chain_instance_details = { - 'service_chain_instance': network_function_data[ - 'service_chain_instance'], - 'provider': network_function_data['provider'], - 'consumer': network_function_data['consumer'] - } - network_function_details.update(service_chain_instance_details) - with nfp_ctx_mgr.DbContextManager as dcm: - network_function = dcm.lock( - self.db_session, - self.db_handler.update_network_function, - network_function_id, network_function) - nfp_context.update(network_function_details) - - LOG.info("[Event:ServiceDeleteInitiated]") - LOG.event("Started delete network function.", - stats_type=nfp_constants.request_event) - if not base_mode_support: - self._create_event('DELETE_NETWORK_FUNCTION_INSTANCE', - event_data=network_function_details, - is_internal_event=True) - - dnf_event = self._controller.new_event( - id='DELETE_NETWORK_FUNCTION_DB', - key=network_function_id) - - GRAPH = {dnf_event: []} - - if network_function['config_policy_id']: - ducf_event = ( - self._controller.new_event(id='DELETE_USER_CONFIG', - key=network_function_id, - serialize=True, - binding_key=network_function_id)) - GRAPH[dnf_event].append(ducf_event) - else: - self.delete_network_function_user_config(network_function_id, - service_config) - if not base_mode_support: - dnfd_event = self._controller.new_event( - id='DELETE_NETWORK_FUNCTION_DEVICE', - key=network_function_id, - serialize=True, - binding_key=network_function_id) - GRAPH[dnf_event].append(dnfd_event) - self._controller.post_graph( - GRAPH, dnf_event, graph_str='DELETE_NETWORK_FUNCTION_GRAPH') - - def delete_user_config(self, event): - network_function_details = event.context - - network_function_info = network_function_details['network_function'] - if not network_function_info['config_policy_id']: - self._controller.event_complete(event, result="SUCCESS") - return - - config_policy_id = self.config_driver.delete_config( - network_function_info['config_policy_id'], - network_function_info['tenant_id'], - network_function_info) - request_data = { - 'config_policy_id': network_function_info['config_policy_id'], - 'tenant_id': network_function_info['tenant_id'], - 'network_function_id': network_function_info['id'], - 'action': 'delete' - } - if not config_policy_id: - # self._create_event('USER_CONFIG_DELETE_FAILED', - # event_data=request_data, is_internal_event=True) - self._controller.event_complete(event, result="FAILED") - return - request_data['event_desc'] = event.desc.to_dict() - self._create_event( - 'DELETE_USER_CONFIG_IN_PROGRESS', - event_data=request_data, - is_poll_event=True, original_event=event, - max_times=nfp_constants.DELETE_USER_CONFIG_IN_PROGRESS_MAXRETRY) - - def _update_nfp_context(self, nfp_context): - provider = nfp_context['provider'] - consumer = nfp_context['consumer'] - provider['pt'] = provider['pt'][0] - provider['ptg'] = provider['ptg'][0] - provider['port'] = provider['port'][0] - if consumer['pt']: - consumer['pt'] = consumer['pt'][0] - if consumer['ptg']: - consumer['ptg'] = consumer['ptg'][0] - if consumer['port']: - consumer['port'] = consumer['port'][0] - - def create_network_function_instance_db(self, event): - nfp_context = event.context - - network_function = nfp_context['network_function'] - service_details = nfp_context['service_details'] - - port_info = [] - # REVISIT(ashu): Only pick few chars from id - name = '%s_%s' % (network_function['id'][:3], - network_function['name']) - create_nfi_request = { - 'name': name, - 'tenant_id': network_function['tenant_id'], - 'status': nfp_constants.PENDING_CREATE, - 'network_function_id': network_function['id'], - 'service_type': service_details['service_type'], - 'service_vendor': service_details['service_vendor'], - 'share_existing_device': nfp_context['share_existing_device'], - 'port_info': port_info, - } - with nfp_ctx_mgr.DbContextManager: - nfi_db = self.db_handler.create_network_function_instance( - self.db_session, create_nfi_request) - # Sending LogMeta Details to visibility - self._report_logging_info(network_function, - nfi_db, - service_details['service_type'], - service_details['service_vendor']) - - nfp_context['network_function_instance'] = nfi_db - - self._update_nfp_context(nfp_context) - - ev = self._controller.new_event( - id='CREATE_NETWORK_FUNCTION_INSTANCE', - key=network_function['id']) - self._controller.post_event(ev) - - self._controller.event_complete(event) - - def create_network_function_instance(self, event): - nfp_context = event.context - network_function = nfp_context['network_function'] - consumer = nfp_context['consumer'] - provider = nfp_context['provider'] - network_function_instance = nfp_context[ - 'network_function_instance'] - port_info = [] - for ele in [consumer, provider]: - if ele['pt']: - # REVISIT(ashu): Only pick few chars from id - port_info.append( - {'id': ele['pt']['id'], - 'port_model': ele['port_model'], - 'port_classification': ele['port_classification'] - }) - - nfi = { - 'port_info': port_info - } - with nfp_ctx_mgr.DbContextManager: - nfi = self.db_handler.update_network_function_instance( - self.db_session, network_function_instance['id'], nfi) - nfp_context['network_function_instance'] = nfi - - nfp_context['log_context']['nfi_id'] = nfi['id'] - - LOG.info("Creating event CREATE NETWORK FUNCTION DEVICE " - "for NF: %(network_function_id)s", - {'network_function_id': network_function['id']}) - - ev = self._controller.new_event( - id='CREATE_NETWORK_FUNCTION_DEVICE', - key=network_function['id'] + nfi['id']) - - if nfp_context.get('binding_key'): - ev.sequence = True - ev.binding_key = nfp_context.get('binding_key') - - LOG.debug("Acquiring tenant based lock for " - "CREATE_NETWORK_FUNCTION_DEVICE event with binding " - "key: %s, sequence: %s", ( - ev.binding_key, ev.sequence)) - self._controller.post_event(ev) - if event.binding_key and not nfp_context.get('is_nfi_in_graph'): - LOG.debug("Releasing lock for CREATE_NETWORK_FUNCTION_INSTANCE" - " event for gateway services sharing with binding key:" - " %s", event.binding_key) - self._controller.event_complete(event) - - def handle_device_created(self, event): - # Not needed for NFP - """ - request_data = event.data - nfi = { - 'network_function_device_id': request_data[ - 'network_function_device_id'] - } - with nfp_ctx_mgr.DbContextManager: - nfi = self.db_handler.update_network_function_instance( - self.db_session, - request_data['network_function_instance_id'], nfi) - self._controller.event_complete(event) - """ - return - - def send_user_config(self, event): - nfp_context = event.context - - network_function_instance = nfp_context['network_function_instance'] - network_function_device = nfp_context['network_function_device'] - network_function = nfp_context['network_function'] - network_function_instance['status'] = nfp_constants.ACTIVE - network_function_instance[ - 'network_function_device_id'] = network_function_device['id'] - # get service_config from nf - service_config = nfp_context['service_chain_node'].get('config') - nfp_context['event_desc'] = event.desc.to_dict() - nfp_context['key'] = event.key - nfp_context['id'] = event.id - self.create_network_function_user_config(network_function['id'], - service_config) - - def handle_device_active(self, event): - request_data = event.data - nfi = { - 'status': nfp_constants.ACTIVE, - 'network_function_device_id': request_data[ - 'network_function_device_id'] - } - with nfp_ctx_mgr.DbContextManager: - nfi = self.db_handler.update_network_function_instance( - self.db_session, - request_data['network_function_instance_id'], nfi) - network_function = self.db_handler.get_network_function( - self.db_session, nfi['network_function_id']) - service_config = network_function['service_config'] - - self.create_network_function_user_config(network_function['id'], - service_config) - - def check_heat_config_result(self, event): - nfp_context = event.data['nfp_context'] - - base_mode = nfp_context['base_mode'] - if base_mode: - # Create and event to apply user config - self._create_event('APPLY_USER_CONFIG_BASEMODE', - event_data=event.data, - is_internal_event=True) - else: - event_desc = nfp_context['event_desc'] - key = nfp_context['key'] - id = nfp_context['id'] - - # Complete this event first - self._controller.event_complete(event) - # Complete the original event here - event = self._controller.new_event(id=id, key=key, - desc_dict=event_desc) - self._controller.event_complete(event, result='SUCCESS') - - def apply_user_config_basemode(self, event): - request_data = event.data - network_function_details = self.get_network_function_details( - request_data['network_function_id']) - request_data['config_policy_id'] = self.config_driver.apply_config( - network_function_details) # Heat driver to launch stack - network_function = network_function_details['network_function'] - request_data['network_function_id'] = network_function['id'] - if not request_data['config_policy_id']: - self._create_event('USER_CONFIG_FAILED', - event_data=request_data, is_internal_event=True) - return - request_data['tenant_id'] = network_function['tenant_id'] - request_data['network_function_details'] = network_function_details - LOG.debug("handle_device_active config_policy_id: %s", - request_data['config_policy_id']) - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock(self.db_session, self.db_handler.update_network_function, - network_function['id'], - {'config_policy_id': request_data['config_policy_id'], - 'description': network_function['description']}) - self._create_event( - 'APPLY_USER_CONFIG_IN_PROGRESS', - event_data=request_data, - is_poll_event=True, - original_event=event, - max_times=nfp_constants.APPLY_USER_CONFIG_IN_PROGRESS_MAXRETRY) - - def initiate_user_config(self, event): - # Split the user config creation in 2 steps, - # get, update the description in network function and - # apply user config - event_results = event.result - for c_event in event_results: - if c_event.id == "SEND_USER_CONFIG" and ( - c_event.result.upper() == "FAILED"): - self._controller.event_complete(event, result="FAILED") - return - elif c_event.id == "SEND_USER_CONFIG" and ( - c_event.result.upper() == "HANDLED"): - self._controller.event_complete( - event, result="SUCCESS") - return - nfp_context = event.context - nfp_context['event_desc'] = event.desc.to_dict() - network_function = nfp_context['network_function'] - ev = self._controller.new_event( - id='UPDATE_NETWORK_FUNCTION_DESCRIPTION', - key=network_function['id']) - self._controller.post_event(ev) - - def update_network_function_description(self, event): - nfp_context = event.context - - network_function = nfp_context['network_function'] - network_function['description'] = str(network_function['description']) - neutron_resource_desc = ( - self.config_driver.get_neutron_resource_description(nfp_context)) - if not neutron_resource_desc: - LOG.error( - "Preparing neutron resource description failed in " - "config driver, marking user config as Failed for " - "network function: %(nf)s", {'nf': network_function}) - nfp_context['network_function_id'] = network_function['id'] - binding_key = nfp_context['service_details'][ - 'service_vendor'].lower() + network_function['id'] - # Complete the original event INITIATE_USER_CONFIG here - event_desc = nfp_context.pop('event_desc', None) - apply_config_event = self._controller.new_event( - id='INITIATE_USER_CONFIG', - key=network_function['id'], - desc_dict=event_desc) - apply_config_event.binding_key = binding_key - self._controller.event_complete( - apply_config_event, result="FAILED") - # self._create_event('USER_CONFIG_FAILED', - # event_data=nfp_context, is_internal_event=True) - return - nf_desc = network_function['description'] + \ - '\n' + neutron_resource_desc - nfp_context['network_function'].update({'description': nf_desc}) - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock(self.db_session, self.db_handler.update_network_function, - network_function['id'], - {'description': nf_desc}) - ev = self._controller.new_event( - id='APPLY_USER_CONFIG', - key=network_function['id']) - self._controller.post_event(ev) - self._controller.event_complete(event) - - def apply_user_config(self, event): - nfp_context = event.context - - network_function = nfp_context['network_function'] - nfp_context['config_policy_id'] = self.config_driver.apply_heat_config( - nfp_context) # Heat driver to launch stack - nfp_context['network_function_id'] = network_function['id'] - if not nfp_context['config_policy_id']: - # self._create_event('USER_CONFIG_FAILED', - # event_data=nfp_context, is_internal_event=True) - binding_key = nfp_context['service_details'][ - 'service_vendor'].lower() + network_function['id'] - # Complete the original event INITIATE_USER_CONFIG here - event_desc = nfp_context.pop('event_desc', None) - apply_config_event = self._controller.new_event( - id='INITIATE_USER_CONFIG', - key=network_function['id'], - desc_dict=event_desc) - apply_config_event.binding_key = binding_key - self._controller.event_complete( - apply_config_event, result="FAILED") - self._controller.event_complete(event, result='FAILED') - return - - LOG.debug("handle_device_active config_policy_id: %s", - nfp_context['config_policy_id']) - nfp_context['network_function'].update( - {'config_policy_id': nfp_context['config_policy_id']}) - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock(self.db_session, self.db_handler.update_network_function, - network_function['id'], - {'config_policy_id': nfp_context['config_policy_id']}) - nfp_context['event_desc'] = event.desc.to_dict() - self._create_event( - 'CHECK_USER_CONFIG_COMPLETE', - is_poll_event=True, - original_event=event, - max_times=nfp_constants.CHECK_USER_CONFIG_COMPLETE_MAXRETRY) - self._controller.event_complete(event) - - def handle_update_user_config(self, event): - ''' - Handler to apply any updates in user config. - Initially checks with config driver whether upadte supported for - service type or not. If not supported first deletes the config(checks - for user config deletion via UPDATE_USER_CONFIG_PREPARING_TO_START - event) and then recreates the config with new changes via - UPDATE_USER_CONFIG_STILL_IN_PROGRESS event. - If update supported, update/create corresponding user config in - UPDATE_USER_CONFIG_IN_PROGRESS event. - - ''' - request_data = event.data - network_function_details = self.get_network_function_details( - request_data['network_function_id']) - network_function = network_function_details['network_function'] - service_profile_id = network_function['service_profile_id'] - service_type = self._get_service_type(service_profile_id) - request_data.update({'service_type': service_type}) - self._controller.event_complete(event) - self._create_event('UPDATE_USER_CONFIG_IN_PROGRESS', - event_data=request_data, - is_internal_event=True, - original_event=event) - - def handle_continue_update_user_config(self, event): - request_data = event.data - network_function_details = self.get_network_function_details( - request_data['network_function_id']) - network_function = network_function_details['network_function'] - - LOG.info("[Event:ServiceUpdateInitiated]") - LOG.event("Started update network function.", - stats_type=nfp_constants.request_event) - nfi = network_function_details.get('network_function_instance', None) - nfd = network_function_details.get('network_function_device', None) - nfi_id = nfi.get('id', '-') if nfi else '-' - nfd_id = nfd.get('id', '-') if nfd else '-' - - nfp_context = event.context - nfp_context['log_context']['nfi_id'] = nfi_id - nfp_context['log_context']['nfd_id'] = nfd_id - - original_stack_id = network_function_details[ - 'network_function']['config_policy_id'] - service_type = request_data['service_type'] - if not self.config_driver.is_update_config_supported(service_type): - network_function_details['network_function'][ - 'config_policy_id'] = None - - if request_data['operation'] == 'update': - config_id = self.config_driver.update_config( - network_function_details, - network_function_details[ - 'network_function']['config_policy_id']) - elif request_data['operation'] == 'consumer_add': - config_id = self.config_driver.handle_consumer_ptg_operations( - network_function_details, request_data['consumer_ptg'], - "add") - elif request_data['operation'] == 'consumer_remove': - config_id = self.config_driver.handle_consumer_ptg_operations( - network_function_details, request_data['consumer_ptg'], - "remove") - else: - return - - if config_id: - request_data = { - 'config_policy_id': config_id, - 'tenant_id': network_function['tenant_id'], - 'network_function_id': network_function['id'], - 'network_function_details': network_function_details, - 'operation': request_data['operation'], - 'stack_id_to_delete': original_stack_id, - 'service_type': service_type - } - else: - event_id = ('USER_CONFIG_UPDATE_FAILED' - if request_data['operation'] == 'update' - else 'USER_CONFIG_FAILED') - self._create_event(event_id, - event_data=request_data, - is_internal_event=True) - if event.binding_key: - self._controller.event_complete(event) - return - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock(self.db_session, self.db_handler.update_network_function, - network_function['id'], - {'config_policy_id': config_id}) - self._create_event( - 'UPDATE_USER_CONFIG_STILL_IN_PROGRESS', - event_data=request_data, - is_poll_event=True, original_event=event, - max_times=self.UPDATE_USER_CONFIG_STILL_IN_PROGRESS_MAXRETRY) - - def handle_device_create_failed(self, event): - request_data = event.data - nfi = { - 'status': nfp_constants.ERROR, - } - with nfp_ctx_mgr.DbContextManager: - nfi = self.db_handler.update_network_function_instance( - self.db_session, - request_data['network_function_instance_id'], nfi) - network_function = {'status': nfp_constants.ERROR} - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock(self.db_session, self.db_handler.update_network_function, - nfi['network_function_id'], - network_function) - nfp_context = event.context - operation = nfp_context['log_context'].get('path') - - LOG.error("[Event:Service%(operation)sFailed]", - {'operation': operation.capitalize()}) - LOG.event('%s network function failed.' % operation.capitalize(), - stats_type=nfp_constants.error_event) - - # Trigger RPC to notify the Create_Service caller with status - - def handle_driver_error(self, network_function_id): - LOG.error("Error occurred while processing network function " - "CRUD operations, marking network function: %(nf_id)s " - "as ERROR to initiate cleanup.", - {'nf_id': network_function_id}) - network_function_details = self.get_network_function_details( - network_function_id) - network_function_id = network_function_details.get( - 'network_function')['id'] - network_function = {'status': nfp_constants.ERROR} - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock(self.db_session, self.db_handler.update_network_function, - network_function_id, network_function) - nfp_context = module_context.get() - operation = nfp_context['log_context'].get('path') - LOG.error("[Event:Service%(operation)sFailed]", - {'operation': operation.capitalize()}) - LOG.event('%s network function failed.' % operation.capitalize(), - stats_type=nfp_constants.error_event) - - if network_function_details.get('network_function_instance'): - network_function_instance_id = network_function_details[ - 'network_function_instance']['id'] - nfi = { - 'status': nfp_constants.ERROR, - } - with nfp_ctx_mgr.DbContextManager: - nfi = self.db_handler.update_network_function_instance( - self.db_session, network_function_instance_id, nfi) - - def _update_network_function_instance(self): - pass - - def delete_network_function_instance(self, event): - network_function_details = event.context - nfi_id = network_function_details['network_function_instance']['id'] - nfi = {'status': nfp_constants.PENDING_DELETE} - with nfp_ctx_mgr.DbContextManager: - nfi = self.db_handler.update_network_function_instance( - self.db_session, nfi_id, nfi) - network_function_details['network_function_instance'] = nfi - - # FIXME: Add all possible validations here - def _validate_create_service_input(self, context, create_service_request): - required_attributes = ["resource_owner_context", - "service_chain_instance", - "service_chain_node", "service_profile", - "service_config", "provider", "consumer", - "network_function_mode"] - if (set(required_attributes) & set(create_service_request.keys()) != - set(required_attributes)): - missing_keys = (set(required_attributes) - - set(create_service_request.keys())) - raise nfp_exc.RequiredDataNotProvided( - required_data=", ".join(missing_keys), - request="Create Network Function") - if create_service_request['network_function_mode'].lower() == "gbp": - gbp_required_attributes = ["management_ptg_id"] - if (set(gbp_required_attributes) & - set(create_service_request.keys()) != - set(gbp_required_attributes)): - missing_keys = (set(gbp_required_attributes) - - set(create_service_request.keys())) - raise nfp_exc.RequiredDataNotProvided( - required_data=", ".join(missing_keys), - request="Create Network Function") - - service_profile = create_service_request['service_profile'] - service_details = transport.parse_service_flavor_string( - service_profile['service_flavor']) - service_vendor = service_details.get('service_vendor') - if (not service_vendor or - not service_details.get('device_type')): - raise Exception(_("service_vendor or device_type not provided in " - "service profile's service flavor field." - "Provided service profile: %s") % service_profile) - self._validate_service_vendor(service_vendor.lower()) - - @nfp_api.poll_event_desc( - event='APPLY_USER_CONFIG_IN_PROGRESS', - spacing=nfp_constants.APPLY_USER_CONFIG_IN_PROGRESS_SPACING) - def apply_user_config_in_progress(self, event): - request_data = event.data - config_status = self.config_driver.is_config_complete( - request_data['config_policy_id'], request_data['tenant_id'], - request_data['network_function_details']) - if config_status == nfp_constants.ERROR: - LOG.info("Applying user config failed for " - "NF:%(network_function_id)s ", { - 'network_function_id': - request_data['network_function_id']}) - updated_network_function = {'status': nfp_constants.ERROR} - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock( - self.db_session, - self.db_handler.update_network_function, - request_data['network_function_id'], - updated_network_function) - operation = event.context['log_context'].get('path') - LOG.error("[Event:Service%(operation)sFailed]", - {'operation': operation.capitalize()}) - LOG.event('%s network function failed.' % operation.capitalize(), - stats_type=nfp_constants.error_event) - - self._controller.event_complete(event) - return STOP_POLLING - # Trigger RPC to notify the Create_Service caller with status - elif config_status == nfp_constants.COMPLETED: - if (request_data.get('operation') in [ - 'consumer_add', - 'consumer_remove', 'update'] and not - self.config_driver.is_update_config_supported( - request_data['service_type'])): - - self.config_driver.delete_config( - request_data['stack_id_to_delete'], - request_data['tenant_id']) - request_data = { - 'config_policy_id': request_data['stack_id_to_delete'], - 'network_function_id': request_data['network_function_id'], - 'tenant_id': request_data['tenant_id'], - 'action': 'update', - 'operation': request_data['operation'], - 'service_type': request_data['service_type'] - } - self._controller.event_complete(event) - self._create_event( - 'UPDATE_USER_CONFIG_PREPARING_TO_START', - event_data=request_data, - is_poll_event=True, original_event=event, - max_times=self.UPDATE_USER_CONFIG_MAXRETRY) - return STOP_POLLING - updated_network_function = {'status': nfp_constants.ACTIVE} - LOG.info("Applying user config is successfull moving " - "NF:%(network_function_id)s to ACTIVE", - {'network_function_id': - request_data['network_function_id']}) - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock( - self.db_session, - self.db_handler.update_network_function, - request_data['network_function_id'], - updated_network_function) - - operation = event.context['log_context'].get('path') - LOG.info("[Event:Service%(operation)sCompleted]", - {'operation': operation.capitalize()}) - LOG.event('Completed %s network function.' % operation, - stats_type=nfp_constants.response_event) - - nf_id = request_data['network_function_id'] - with nfp_ctx_mgr.DbContextManager: - network_function = self.db_handler.get_network_function( - self.db_session, nf_id) - service_profile_id = network_function['service_profile_id'] - # Revisit(shabbir): service_type should be passed from previous - # event - service_type = self._get_service_type(service_profile_id) - LOG.event('Completed %s network function.' % operation, - type='SERVICE_UPDATED', - nf_id=nf_id, - service_type=service_type) - - self._controller.event_complete(event) - return STOP_POLLING - # Trigger RPC to notify the Create_Service caller with status - elif config_status == nfp_constants.IN_PROGRESS: - return CONTINUE_POLLING - - def handle_service_configured(self, event): - nfp_context = event.context - - network_function = nfp_context['network_function'] - updated_network_function = {'status': nfp_constants.ACTIVE} - LOG.info("Applying user config is successfull moving " - "NF: %(network_function_id)s to ACTIVE", - {'network_function_id': network_function['id']}) - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock(self.db_session, self.db_handler.update_network_function, - network_function['id'], - updated_network_function) - operation = nfp_context['log_context'].get('path') - - LOG.info("[Event:Service%(operation)sCompleted]", - {'operation': operation.capitalize()}) - LOG.event('Completed %s network function.' % operation, - stats_type=nfp_constants.response_event) - - service_type = nfp_context['service_details']['service_type'] - nf_id = network_function['id'] - LOG.event("Sending service created event to controller.", - type='SERVICE_CREATED', - nf_id=nf_id, - service_type=service_type) - - self._controller.event_complete(event) - - @nfp_api.poll_event_desc( - event='CHECK_USER_CONFIG_COMPLETE', - spacing=nfp_constants.CHECK_USER_CONFIG_COMPLETE_SPACING) - def check_for_user_config_complete(self, event): - nfp_context = event.context - - network_function = nfp_context['network_function'] - binding_key = nfp_context[ - 'service_details'][ - 'service_vendor'].lower() + network_function['id'] - config_status = self.config_driver.check_config_complete(nfp_context) - if config_status == nfp_constants.ERROR: - - LOG.info("Applying user config failed for " - "NF: %(network_function_id)s", { - 'network_function_id': - network_function['id']}) - # Complete the original event APPLY_USER_CONFIG here - event_desc = nfp_context.pop('event_desc', None) - apply_config_event = self._controller.new_event( - id='INITIATE_USER_CONFIG', - key=network_function['id'], - desc_dict=event_desc) - apply_config_event.binding_key = binding_key - self._controller.event_complete( - apply_config_event, result="FAILED") - return STOP_POLLING - # Trigger RPC to notify the Create_Service caller with status - elif config_status == nfp_constants.COMPLETED: - # Complete the original event DEVICE_ACTIVE here - event_desc = nfp_context.pop('event_desc', None) - apply_config_event = self._controller.new_event( - id='INITIATE_USER_CONFIG', - key=network_function['id'], - desc_dict=event_desc) - apply_config_event.binding_key = binding_key - self._controller.event_complete( - apply_config_event, result="SUCCESS") - - return STOP_POLLING - # Trigger RPC to notify the Create_Service caller with status - elif config_status == nfp_constants.IN_PROGRESS: - return CONTINUE_POLLING - - @nfp_api.poll_event_desc( - event='UPDATE_USER_CONFIG_PREPARING_TO_START', - spacing=nfp_constants.UPDATE_USER_CONFIG_PREPARING_TO_START_SPACING) - def check_for_user_config_deleted(self, event): - request_data = event.data - try: - with nfp_ctx_mgr.DbContextManager: - network_function = self.db_handler.get_network_function( - self.db_session, - request_data['network_function_id']) - config_status = self.config_driver.is_config_delete_complete( - request_data['config_policy_id'], request_data['tenant_id'], - network_function) - except Exception as err: - # REVISIT: May be we need a count before removing the poll event - LOG.error("Error: %(err)s while verifying configuration " - "delete completion.", {'err': err}) - self._create_event('USER_CONFIG_DELETE_FAILED', - event_data=request_data, is_internal_event=True) - self._controller.event_complete(event) - return STOP_POLLING - service_profile_id = network_function['service_profile_id'] - # Revisit(shabbir): service_type should be passed from previous event - service_type = self._get_service_type(service_profile_id) - if config_status == nfp_constants.ERROR: - self._create_event('USER_CONFIG_DELETE_FAILED', - event_data=request_data, is_internal_event=True) - self._controller.event_complete(event) - return STOP_POLLING - # Trigger RPC to notify the Create_Service caller with status - elif config_status == nfp_constants.COMPLETED: - updated_network_function = {'status': nfp_constants.ACTIVE} - LOG.info("Applying user config is successfull moving " - "NF:%(network_function_id)s to ACTIVE", - {'network_function_id': - request_data['network_function_id']}) - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock( - self.db_session, - self.db_handler.update_network_function, - request_data['network_function_id'], - updated_network_function) - operation = event.context['log_context'].get('path') - - LOG.info("[Event:Service%(operation)sCompleted]", - {'operation': operation.capitalize()}) - LOG.event('Completed %s network function.' % operation, - stats_type=nfp_constants.response_event) - - nf_id = request_data['network_function_id'] - LOG.event("Sending service updated event to controller.", - type='SERVICE_UPDATED', - nf_id=nf_id, - service_type=service_type) - - self._controller.event_complete(event) - return STOP_POLLING - # Trigger RPC to notify the Create_Service caller with status - elif config_status == nfp_constants.IN_PROGRESS: - return CONTINUE_POLLING - - @nfp_api.poll_event_desc( - event='DELETE_USER_CONFIG_IN_PROGRESS', - spacing=nfp_constants.DELETE_USER_CONFIG_IN_PROGRESS_SPACING) - def check_for_user_config_deleted_fast(self, event): - request_data = event.data - nf_id = request_data['network_function_id'] - try: - config_status = self.config_driver.is_config_delete_complete( - request_data['config_policy_id'], request_data['tenant_id']) - except Exception as err: - # REVISIT: May be we need a count before removing the poll event - LOG.error("Error: %(err)s while verifying configuration " - "delete completion.", {'err': err}) - # self._create_event('USER_CONFIG_DELETE_FAILED', - # event_data=event_data, is_internal_event=True) - self._controller.event_complete(event) - ducf_event = self._controller.new_event( - id='DELETE_USER_CONFIG', - key=nf_id, - binding_key=nf_id, - desc_dict=request_data['event_desc']) - self._controller.event_complete(ducf_event, result="FAILED") - - return STOP_POLLING - if config_status == nfp_constants.ERROR: - # self._create_event('USER_CONFIG_DELETE_FAILED', - # event_data=event_data, is_internal_event=True) - self._controller.event_complete(event) - ducf_event = self._controller.new_event( - id='DELETE_USER_CONFIG', - key=nf_id, - binding_key=nf_id, - desc_dict=request_data['event_desc']) - self._controller.event_complete(ducf_event, result="FAILED") - return STOP_POLLING - # Trigger RPC to notify the Create_Service caller with status - elif config_status == nfp_constants.COMPLETED: - self._controller.event_complete(event) - ducf_event = self._controller.new_event( - id='DELETE_USER_CONFIG', - key=nf_id, - binding_key=nf_id, - desc_dict=request_data['event_desc']) - self._controller.event_complete(ducf_event, result="SUCCESS") - return STOP_POLLING - # Trigger RPC to notify the Create_Service caller with status - elif config_status == nfp_constants.IN_PROGRESS: - return CONTINUE_POLLING - - def handle_user_config_applied(self, event): - request_data = event.data - network_function = { - 'status': nfp_constants.ACTIVE, - 'config_policy_id': request_data['config_policy_id'] - } - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock(self.db_session, self.db_handler.update_network_function, - request_data['network_function_id'], - network_function) - # Trigger RPC to notify the Create_Service caller with status - - def handle_config_applied(self, event): - nfp_context = event.data['nfp_context'] - base_mode = nfp_context['base_mode'] - network_function_id = event.data['network_function_id'] - if base_mode: - network_function = { - 'status': nfp_constants.ACTIVE, - } - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock( - self.db_session, - self.db_handler.update_network_function, - network_function_id, - network_function) - LOG.info("Applying user config is successfull moving " - "NF: %(network_function_id)s to ACTIVE", - {'network_function_id': - network_function_id}) - else: - ''' - network_function_instance_id = ( - event.data['network_function_instance_id']) - if network_function_instance_id: - nfi = { - 'status': nfp_constants.ACTIVE, - } - nfi = self.db_handler.update_network_function_instance( - self.db_session, network_function_instance_id, nfi) - ''' - event_desc = nfp_context['event_desc'] - key = nfp_context['key'] - id = nfp_context['id'] - - # Complete the original event here - event = self._controller.new_event(id=id, key=key, - desc_dict=event_desc) - self._controller.event_complete(event, result='HANDLED') - - def handle_update_user_config_failed(self, event): - event_data = event.data - network_function_id = event_data['network_function_id'] - LOG.error("NSO: updating user config failed, moving " - "network function %(network_function_id)s to ERROR", - {'network_function_id': network_function_id}) - self.handle_user_config_failed(event) - - def handle_user_config_failed(self, event): - request_data = event.data - updated_network_function = { - 'status': nfp_constants.ERROR, - 'config_policy_id': request_data.get('config_policy_id') - } - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock(self.db_session, self.db_handler.update_network_function, - request_data['network_function_id'], - updated_network_function) - # Trigger RPC to notify the Create_Service caller with status - operation = event.context['log_context'].get('path') - LOG.error("[Event:Service%(operation)sFailed]", - {'operation': operation.capitalize()}) - LOG.event('%s network function failed.' % operation.capitalize(), - stats_type=nfp_constants.error_event) - - def handle_user_config_deleted(self, event): - # DELETE DEVICE_CONFIGURATION is not serialized with DELETE - # SERVICE_CONFIGURATION so,no logic need to be added here. - pass - - # Change to Delete_failed or continue with instance and device - # delete if config delete fails? or status CONFIG_DELETE_FAILED ?? - def handle_user_config_delete_failed(self, event): - request_data = event.data - updated_network_function = { - 'status': nfp_constants.ERROR, - } - # If stack delete fails after successfull heat stack create - # in fw update case - # still we make network function status active to allow subsequent - # sharing - if (request_data.get('operation') in [ - 'consumer_add', 'consumer_remove', 'update'] and not - self.config_driver.is_update_config_supported( - request_data['service_type'])): - updated_network_function.update({'status': nfp_constants.ACTIVE}) - LOG.warning( - "Failed to delete old stack id: %(stack_id)s in" - "firewall update case, Need to manually delete it", - {"stack_id": request_data['config_policy_id']}) - - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock(self.db_session, self.db_handler.update_network_function, - request_data['network_function_id'], - updated_network_function) - # Trigger RPC to notify the Create_Service caller with status - - # When NDO deletes Device DB, the Foreign key NSI will be nulled - # So we have to pass the NSI ID in delete event to NDO and process - # the result based on that - def delete_network_function_db(self, event): - results = event.result - for result in results: - if result.result.lower() != 'success': - LOG.error("Event: %(result_id)s failed", - {'result_id': result.id}) - - network_function_details = event.context - if not network_function_details['base_mode_support']: - nfi_id = ( - network_function_details['network_function_instance']['id']) - with nfp_ctx_mgr.DbContextManager.new( - suppress=( - nfp_exc.NetworkFunctionInstanceNotFound,)): - - self.db_handler.delete_network_function_instance( - self.db_session, nfi_id) - - nf_id = network_function_details['network_function']['id'] - with nfp_ctx_mgr.DbContextManager: - nf = self.db_handler.get_network_function( - self.db_session, nf_id) - - if not nf['network_function_instances']: - with nfp_ctx_mgr.DbContextManager: - self.db_handler.delete_network_function( - self.db_session, nf['id']) - LOG.info("[Event:ServiceDeleteCompleted]") - LOG.event("Completed delete network function.", - stats_type=nfp_constants.response_event) - - service_type = network_function_details['service_profile'][ - 'service_type'] - LOG.event("Sending service deleted event to controller.", - type='SERVICE_DELETED', - nf_id=nf_id, - service_type=service_type) - - LOG.info("Deleted NF:%(nf_id)s ", - {'nf_id': nf['id']}) - self._controller.event_complete(event) - - def handle_device_deleted(self, event): - request_data = event.data - nfi_id = request_data['network_function_instance_id'] - with nfp_ctx_mgr.DbContextManager: - nfi = self.db_handler.get_network_function_instance( - self.db_session, nfi_id) - self.db_handler.delete_network_function_instance( - self.db_session, nfi_id) - network_function = self.db_handler.get_network_function( - self.db_session, nfi['network_function_id']) - nf_id = network_function['id'] - if not network_function['network_function_instances']: - with nfp_ctx_mgr.DbContextManager: - self.db_handler.delete_network_function( - self.db_session, nfi['network_function_id']) - LOG.info("[Event:ServiceDeleteCompleted]") - LOG.event("Completed delete network function.", - stats_type=nfp_constants.response_event) - - service_type = request_data['service_type'] - LOG.event("Sending service deleted event to controller.", - type='SERVICE_DELETED', - nf_id=nf_id, - service_type=service_type) - - LOG.info("Deleted NF:%(nf_id)s ", - {'nf_id': nf_id}) - # Inform delete service caller with delete completed RPC - - def get_network_function(self, context, network_function_id): - try: - nfp_context = module_context.get() - nfp_context['log_context']['meta_id'] = network_function_id - nfp_context['log_context']['auth_token'] = context.auth_token - - with nfp_ctx_mgr.DbContextManager: - network_function = self.db_handler.get_network_function( - self.db_session, network_function_id) - return network_function - except nfp_exc.NetworkFunctionNotFound: - LOG.warning("Failed to retrieve Network Function details for" - " %(network_function)s", - {'network_function': network_function_id}) - return None - except Exception: - LOG.exception("Failed to retrieve Network Function details for" - " %(network_function)s", - {'network_function': network_function_id}) - return None - - def get_network_functions(self, context, filters): - with nfp_ctx_mgr.DbContextManager: - return self.db_handler.get_network_functions( - self.db_session, filters) - - def _update_network_function_status(self, network_function_id, operation): - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock(self.db_session, self.db_handler.update_network_function, - network_function_id, - {'status': self.status_map[operation]['status'], - 'status_description': self.status_map[operation][ - 'status_description']}) - - def handle_policy_target_added(self, context, network_function_id, - policy_target): - nfp_context = module_context.get() - nfp_path.update_path(network_function_id) - nfp_context['event_desc']['path_type'] = 'update' - nfp_context['event_desc']['path_key'] = network_function_id - nfp_context['log_context']['path'] = 'update' - nfp_context['log_context']['meta_id'] = network_function_id - nfp_context['log_context']['auth_token'] = context.auth_token - - with nfp_ctx_mgr.DbContextManager: - network_function = self.db_handler.get_network_function( - self.db_session, network_function_id) - network_function_details = self.get_network_function_details( - network_function_id) - base_mode_support, _ = self._get_base_mode_support( - network_function['service_profile_id']) - if not base_mode_support: - required_attributes = ["network_function", - "network_function_instance", - "network_function_device"] - else: - required_attributes = ["network_function"] - if (set(required_attributes) & set(network_function_details.keys()) != - set(required_attributes)): - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock( - self.db_session, - self.db_handler.update_network_function, - network_function['id'], - {'status': nfp_constants.ERROR, - 'status_description': ( - "Config Update for Policy Target " - "addition event failed")}) - return - self._update_network_function_status(network_function['id'], - operation='pt_add') - service_config = network_function['service_config'] - service_type = self._get_service_type( - network_function['service_profile_id']) - network_function_data = { - 'network_function_details': network_function_details, - 'policy_target': policy_target, - 'service_type': service_type - } - self.pt_add_user_config(network_function_data, - service_config) - - def policy_target_add_user_config(self, event): - request_data = event.data - network_function_details = self.get_network_function_details( - request_data['network_function_id']) - policy_target = request_data['policy_target'] - config_id = self.config_driver.handle_policy_target_operations( - network_function_details, policy_target, "add") - network_function = network_function_details['network_function'] - request_data = { - 'config_policy_id': config_id, - 'tenant_id': network_function['tenant_id'], - 'network_function_id': network_function['id'], - 'network_function_details': network_function_details - } - if not config_id: - self._controller.event_complete(event) - self._create_event('USER_CONFIG_FAILED', - event_data=request_data, is_internal_event=True) - return - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock(self.db_session, self.db_handler.update_network_function, - network_function['id'], - {'config_policy_id': config_id}) - self._controller.event_complete(event) - self._create_event( - 'APPLY_USER_CONFIG_IN_PROGRESS', - event_data=request_data, - is_poll_event=True, original_event=event, - max_times=nfp_constants.APPLY_USER_CONFIG_IN_PROGRESS_MAXRETRY) - - def handle_policy_target_removed(self, context, network_function_id, - policy_target): - nfp_context = module_context.get() - nfp_path.update_path(network_function_id) - nfp_context['event_desc']['path_type'] = 'update' - nfp_context['event_desc']['path_key'] = network_function_id - nfp_context['log_context']['path'] = 'update' - nfp_context['log_context']['meta_id'] = network_function_id - nfp_context['log_context']['auth_token'] = context.auth_token - - with nfp_ctx_mgr.DbContextManager: - network_function = self.db_handler.get_network_function( - self.db_session, network_function_id) - network_function_details = self.get_network_function_details( - network_function_id) - base_mode_support, _ = self._get_base_mode_support( - network_function['service_profile_id']) - if not base_mode_support: - required_attributes = ["network_function", - "network_function_instance", - "network_function_device"] - else: - required_attributes = ["network_function"] - if (set(required_attributes) & set(network_function_details.keys()) != - set(required_attributes)): - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock( - self.db_session, - self.db_handler.update_network_function, - network_function['id'], - {'status': nfp_constants.ERROR, - 'status_description': ( - "Config Update for Policy Target " - "removed event failed")}) - return - self._update_network_function_status(network_function['id'], - operation='pt_remove') - service_config = network_function['service_config'] - service_type = self._get_service_type( - network_function['service_profile_id']) - network_function_data = { - 'network_function_details': network_function_details, - 'policy_target': policy_target, - 'service_type': service_type - } - self.pt_remove_user_config(network_function_data, - service_config) - - def policy_target_remove_user_config(self, event): - request_data = event.data - network_function_details = self.get_network_function_details( - request_data['network_function_id']) - policy_target = request_data['policy_target'] - config_id = self.config_driver.handle_policy_target_operations( - network_function_details, policy_target, "remove") - network_function = network_function_details['network_function'] - request_data = { - 'config_policy_id': config_id, - 'tenant_id': network_function['tenant_id'], - 'network_function_id': network_function['id'], - 'network_function_details': network_function_details - } - if not config_id: - self._controller.event_complete(event) - self._create_event('USER_CONFIG_FAILED', - event_data=request_data, is_internal_event=True) - return - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock(self.db_session, self.db_handler.update_network_function, - network_function['id'], - {'config_policy_id': config_id}) - - self._controller.event_complete(event) - self._create_event( - 'APPLY_USER_CONFIG_IN_PROGRESS', - event_data=request_data, - is_poll_event=True, original_event=event, - max_times=nfp_constants.APPLY_USER_CONFIG_IN_PROGRESS_MAXRETRY) - - def handle_consumer_ptg_added(self, context, network_function_id, - consumer_ptg): - nfp_context = module_context.get() - nfp_path.update_path(network_function_id) - nfp_context['event_desc']['path_type'] = 'update' - nfp_context['event_desc']['path_key'] = network_function_id - nfp_context['log_context']['path'] = 'update' - nfp_context['log_context']['meta_id'] = network_function_id - nfp_context['log_context']['auth_token'] = context.auth_token - - with nfp_ctx_mgr.DbContextManager: - network_function = self.db_handler.get_network_function( - self.db_session, network_function_id) - network_function_details = self.get_network_function_details( - network_function_id) - base_mode_support, _ = self._get_base_mode_support( - network_function['service_profile_id']) - if not base_mode_support: - required_attributes = ["network_function", - "network_function_instance", - "network_function_device"] - else: - required_attributes = ["network_function"] - if (set(required_attributes) & set(network_function_details.keys()) != - set(required_attributes)): - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock(self.db_session, - self.db_handler.update_network_function, - network_function['id'], - {'status': nfp_constants.ERROR, - 'status_description': ( - "Config Update for Consumer Policy" - " Target Group Addition failed")}) - return - self._update_network_function_status(network_function['id'], - operation='ptg_add') - service_config = network_function['service_config'] - service_type = self._get_service_type( - network_function['service_profile_id']) - network_function_data = { - 'network_function_details': network_function_details, - 'consumer_ptg': consumer_ptg, - 'service_type': service_type - } - self.consumer_add_user_config(network_function_data, - service_config) - - def consumer_ptg_add_user_config(self, event): - request_data = event.data - network_function_details = self.get_network_function_details( - request_data['network_function_id']) - network_function = network_function_details['network_function'] - service_profile_id = network_function['service_profile_id'] - service_type = self._get_service_type(service_profile_id) - request_data.update({'service_type': service_type}) - self._controller.event_complete(event) - self._create_event('UPDATE_USER_CONFIG_IN_PROGRESS', - event_data=request_data, - is_internal_event=True, - original_event=event) - - def handle_consumer_ptg_removed(self, context, network_function_id, - consumer_ptg): - nfp_context = module_context.get() - nfp_path.update_path(network_function_id) - nfp_context['event_desc']['path_type'] = 'update' - nfp_context['event_desc']['path_key'] = network_function_id - nfp_context['log_context']['path'] = 'update' - nfp_context['log_context']['meta_id'] = network_function_id - nfp_context['log_context']['auth_token'] = context.auth_token - - with nfp_ctx_mgr.DbContextManager: - network_function = self.db_handler.get_network_function( - self.db_session, network_function_id) - - network_function_details = self.get_network_function_details( - network_function_id) - base_mode_support, _ = self._get_base_mode_support( - network_function['service_profile_id']) - if not base_mode_support: - required_attributes = ["network_function", - "network_function_instance", - "network_function_device"] - else: - required_attributes = ["network_function"] - if (set(required_attributes) & set(network_function_details.keys()) != - set(required_attributes)): - with nfp_ctx_mgr.DbContextManager as dcm: - dcm.lock( - self.db_session, - self.db_handler.update_network_function, - network_function['id'], - {'status': nfp_constants.ERROR, - 'status_description': ( - "Config Update for Consumer Policy" - " Target Group Removal failed")}) - return - self._update_network_function_status(network_function['id'], - operation='ptg_remove') - service_config = network_function['service_config'] - service_type = self._get_service_type( - network_function['service_profile_id']) - network_function_data = { - 'network_function_details': network_function_details, - 'consumer_ptg': consumer_ptg, - 'service_type': service_type - } - self.consumer_remove_user_config(network_function_data, - service_config) - - def consumer_ptg_remove_user_config(self, event): - request_data = event.data - network_function_details = self.get_network_function_details( - request_data['network_function_id']) - network_function = network_function_details['network_function'] - service_profile_id = network_function['service_profile_id'] - service_type = self._get_service_type(service_profile_id) - request_data.update({'service_type': service_type}) - self._controller.event_complete(event) - self._create_event('UPDATE_USER_CONFIG_IN_PROGRESS', - event_data=request_data, - is_internal_event=True, - original_event=event) - - def get_port_info(self, port_id): - try: - with nfp_ctx_mgr.DbContextManager: - port_info = self.db_handler.get_port_info( - self.db_session, port_id) - return port_info - except Exception: - LOG.exception("Failed to retrieve Port Info for" - " %(port_id)s", - {'port_id': port_id}) - return None - - def get_network_function_details(self, network_function_id): - network_function = None - network_function_instance = None - network_function_device = None - service_type = None - - nfp_context = module_context.get() - if nfp_context: - network_function = nfp_context.get('network_function', None) - network_function_instance = nfp_context.get( - 'network_function_instance', None) - network_function_device = nfp_context.get( - 'network_function_device', None) - service_details = nfp_context.get('service_details', None) - if service_details: - service_type = service_details.get('service_type', None) - if not network_function: - with nfp_ctx_mgr.DbContextManager: - network_function = self.db_handler.get_network_function( - self.db_session, network_function_id) - - network_function_details = { - 'network_function': network_function, - 'service_type': service_type - } - - if not network_function_instance: - network_function_instances = network_function[ - 'network_function_instances'] - if not network_function_instances: - return network_function_details - # Assuming single network_function_instance - with nfp_ctx_mgr.DbContextManager: - network_function_instance = ( - self.db_handler.get_network_function_instance( - self.db_session, network_function_instances[0])) - - network_function_details[ - 'network_function_instance'] = network_function_instance - - if not network_function_device: - if network_function_instance['network_function_device_id']: - with nfp_ctx_mgr.DbContextManager: - network_function_device = ( - self.db_handler.get_network_function_device( - self.db_session, - network_function_instance[ - 'network_function_device_id'])) - network_function_details['network_function_device'] = ( - network_function_device) - return network_function_details - - def get_network_function_context(self, network_function_id): - network_function_details = self.get_network_function_details( - network_function_id) - network_function_device = ( - network_function_details['network_function_device']) - ports_info = [] - for id in network_function_details[ - 'network_function_instance']['port_info']: - port_info = self.get_port_info(id) - ports_info.append(port_info) - - mngmt_port_info = None - monitor_port_info = None - if network_function_device: - mgmt_port_id = network_function_device['mgmt_port_id'] - if mgmt_port_id is not None: - mngmt_port_info = self.get_port_info(mgmt_port_id) - - monitor_port_id = network_function_device['monitoring_port_id'] - if monitor_port_id is not None: - monitor_port_info = self.get_port_info(monitor_port_id) - - nf_context = {'network_function_details': network_function_details, - 'ports_info': ports_info, - 'mngmt_port_info': mngmt_port_info, - 'monitor_port_info': monitor_port_info} - return nf_context - - def get_pt_info_for_plumbing(self, chain_info): - plumbing_request = {'management': [], 'provider': [{}], - 'consumer': [{}]} - service_type = chain_info['profile']['service_type'] - if service_type.lower() in GATEWAY_SERVICES: - plumbing_request['plumbing_type'] = nfp_constants.GATEWAY_TYPE - else: - plumbing_request['plumbing_type'] = nfp_constants.ENDPOINT_TYPE - return plumbing_request - - -class NSOConfiguratorRpcApi(object): - - """Service Manager side of the Service Manager to Service agent RPC API""" - API_VERSION = '1.0' - target = oslo_messaging.Target(version=API_VERSION) - - def __init__(self, context, conf): - super(NSOConfiguratorRpcApi, self).__init__() - self.conf = conf - self.context = context - self.client = n_rpc.get_client(self.target) - self.rpc_api = self.client.prepare( - version=self.API_VERSION, - topic=nfp_rpc_topics.NFP_NSO_CONFIGURATOR_TOPIC) - - def _get_request_info(self, user_config_data, operation): - network_function_details = user_config_data[ - 'network_function_details'] - network_function_instance = network_function_details.get( - 'network_function_instance') - nfp_context = module_context.get() - rpc_nfp_context = None - if nfp_context: - rpc_nfp_context = { - 'event_desc': nfp_context.get('event_desc', None), - 'key': nfp_context.pop('key', None), - 'id': nfp_context.pop('id', None), - 'base_mode': nfp_context.pop('base_mode', None)} - nf_data = { - 'service_chain_instance': nfp_context.get( - 'service_chain_instance'), - 'provider': nfp_context.get('provider'), - 'consumer': nfp_context.get('consumer') - } - rpc_nfp_context.update(nf_data) - request_info = { - 'nf_id': network_function_details['network_function']['id'], - 'nfi_id': (network_function_instance['id'] - if network_function_instance else ''), - 'nfd_id': None, - 'requester': nfp_constants.SERVICE_ORCHESTRATOR, - 'operation': operation, - 'logging_context': nfp_context['log_context'], - 'nfp_context': rpc_nfp_context - } - if operation in ['consumer_add', 'consumer_remove']: - request_info.update({'consumer_ptg': user_config_data[ - 'consumer_ptg']}) - elif operation in ['pt_add', 'pt_remove']: - request_info.update({'policy_target': user_config_data[ - 'policy_target']}) - - nfd = network_function_details.get('network_function_device') - if nfd: - request_info['nfd_id'] = network_function_details[ - 'network_function_device']['id'] - nfd_ip = nfd['mgmt_ip_address'] - request_info.update({'device_ip': nfd_ip}) - return request_info - - def _update_params(self, user_config_data, config_params, operation): - request_info = self._get_request_info(user_config_data, operation) - config_params['info']['context'] = request_info - - def create_request_structure(self, user_config_data, - service_config, config_tag): - config_params = { - 'info': { - 'context': None, - 'service_type': user_config_data['service_type'].lower(), - 'service_vendor': None - }, - 'config': [{ - 'resource': nfp_constants.CONFIG_TAG_RESOURCE_MAP[config_tag], - 'resource_data': { - 'config_string': service_config, - } - }] - } - return config_params - - def create_network_function_user_config(self, user_config_data, - service_config, config_tag): - config_params = self.create_request_structure(user_config_data, - service_config, - config_tag) - self._update_params(user_config_data, - config_params, operation='create') - LOG.info("Sending create heat config request to configurator ") - LOG.debug("Sending create heat config request to configurator " - "with config_params = %s", - config_params) - - transport.send_request_to_configurator(self.conf, - self.context, - config_params, - 'CREATE') - - def delete_network_function_user_config(self, user_config_data, - service_config, config_tag): - config_params = self.create_request_structure(user_config_data, - service_config, - config_tag) - self._update_params(user_config_data, - config_params, operation='delete') - LOG.info("Sending delete heat config request to configurator ") - LOG.debug("Sending delete heat config request to configurator " - " with config_params = %s", - config_params) - transport.send_request_to_configurator(self.conf, - self.context, - config_params, - 'DELETE') - - def update_network_function_user_config(self, user_config_data, - service_config, config_tag): - config_params = self.create_request_structure(user_config_data, - service_config, - config_tag) - self._update_params(user_config_data, - config_params, operation='update') - LOG.info("Sending update heat config request to configurator. ") - - transport.send_request_to_configurator(self.conf, - self.context, - config_params, - 'UPDATE') - - def policy_target_add_user_config(self, user_config_data, - service_config, config_tag): - config_params = self.create_request_structure(user_config_data, - service_config, - config_tag) - self._update_params(user_config_data, - config_params, operation='pt_add') - LOG.info("Sending Policy Target and heat config request to " - "configurator .") - - transport.send_request_to_configurator(self.conf, - self.context, - config_params, - 'CREATE') - - def policy_target_remove_user_config(self, user_config_data, - service_config, config_tag): - config_params = self.create_request_structure(user_config_data, - service_config, - config_tag) - self._update_params(user_config_data, - config_params, operation='pt_remove') - LOG.info("Sending Policy Target remove heat config request to " - "configurator. ") - - transport.send_request_to_configurator(self.conf, - self.context, - config_params, - 'DELETE') - - def consumer_add_user_config(self, user_config_data, - service_config, config_tag): - config_params = self.create_request_structure(user_config_data, - service_config, - config_tag) - self._update_params(user_config_data, - config_params, operation='consumer_add') - LOG.info("Sending consumer and heat config request to " - "configurator .") - - transport.send_request_to_configurator(self.conf, - self.context, - config_params, - 'CREATE') - - def consumer_remove_user_config(self, user_config_data, - service_config, config_tag): - config_params = self.create_request_structure(user_config_data, - service_config, - config_tag) - self._update_params(user_config_data, - config_params, operation='consumer_remove') - LOG.info("Sending consumer remove heat config request to " - "configurator .") - - transport.send_request_to_configurator(self.conf, - self.context, - config_params, - 'DELETE') - - -class ExceptionHandler(object): - - @staticmethod - def event_method_mapping(event_id): - event_handler_mapping = { - "CREATE_NETWORK_FUNCTION_INSTANCE_DB": ( - ExceptionHandler.handle_create_nfi_db_exception), - "CREATE_NETWORK_FUNCTION_INSTANCE": ( - ExceptionHandler.handle_create_nfi_exception), - "DEVICE_CREATED": ExceptionHandler.handle_device_created_exception, - "SEND_USER_CONFIG": - ExceptionHandler.handle_send_heat_config_exception, - "APPLY_USER_CONFIG": - ExceptionHandler.handle_apply_user_config_exception, - "APPLY_USER_CONFIG_BASEMODE": - ExceptionHandler.handle_apply_user_config_basemode_exception, - "CHECK_HEAT_CONFIG_RESULT": - ExceptionHandler.handle_check_heat_config_result_exception, - "INITIATE_USER_CONFIG": - ExceptionHandler.handle_initiate_user_config_exception, - "UPDATE_NETWORK_FUNCTION_DESCRIPTION": ( - ExceptionHandler.handle_update_nf_description_exception), - "CHECK_USER_CONFIG_COMPLETE": ( - ExceptionHandler.handle_check_user_config_complete_exception), - "SERVICE_CONFIGURED": ( - ExceptionHandler.handle_service_configured_exception), - "CONFIG_APPLIED": ExceptionHandler.handle_config_applied_exception, - "DEVICE_CREATE_FAILED": ( - ExceptionHandler.handle_device_create_failed_exception), - "DELETE_NETWORK_FUNCTION_INSTANCE": ( - ExceptionHandler.handle_delete_nfi_exception), - "DELETE_USER_CONFIG": ( - ExceptionHandler.handle_delete_user_config_exception), - "DELETE_USER_CONFIG_IN_PROGRESS": ( - ExceptionHandler.handle_check_user_config_deleted_exception), - "DELETE_NETWORK_FUNCTION_DB": ( - ExceptionHandler.handle_delete_network_function_db_exception), - } - if event_id not in event_handler_mapping: - raise Exception(_("Invalid Event ID")) - else: - return event_handler_mapping[event_id] - - @staticmethod - def handle(orchestrator, event, exception): - exc_type, exc_value, exc_traceback = sys.exc_info() - message = "Traceback: %s" % traceback.format_exception( - exc_type, exc_value, exc_traceback) - LOG.error(message) - - exception_handler = ExceptionHandler.event_method_mapping(event.id) - return exception_handler(orchestrator, event, exception) - - @staticmethod - def handle_create_nfi_db_exception(orchestrator, event, exception): - nfp_context = event.context - network_function = nfp_context['network_function'] - orchestrator.db_handler.update_network_function( - orchestrator.db_session, - network_function['id'], - {'status': nfp_constants.ERROR}) - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def handle_create_nfi_exception(orchestrator, event, exception): - nfp_context = event.context - network_function = nfp_context['network_function'] - orchestrator.db_handler.update_network_function( - orchestrator.db_session, - network_function['id'], - {'status': nfp_constants.ERROR}) - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def handle_device_created_exception(orchestrator, event, exception): - device = event.data - network_function_id = device['network_function_id'] - orchestrator.db_handler.update_network_function( - orchestrator.db_session, - network_function_id, - {'status': nfp_constants.ERROR}) - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def handle_send_heat_config_exception(orchestrator, event, exception): - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def handle_check_heat_config_result_exception( - orchestrator, event, exception): - - nfp_context = event.data['nfp_context'] - base_mode = nfp_context['base_mode'] - if base_mode: - network_function = nfp_context['network_function'] - orchestrator.db_handler.update_network_function( - orchestrator.db_session, - network_function['id'], - {'status': nfp_constants.ERROR}) - orchestrator._controller.event_complete(event, result='FAILED') - return - event_desc = nfp_context['event_desc'] - key = nfp_context['key'] - id = nfp_context['id'] - # Complete the original event here - ev = orchestrator._controller.new_event(id=id, key=key, - desc_dict=event_desc) - orchestrator._controller.event_complete(ev, result='FAILED') - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def handle_apply_user_config_exception(orchestrator, event, exception): - nfp_context = event.context - network_function = nfp_context['network_function'] - event_desc = nfp_context.pop('event_desc', None) - apply_config_event = orchestrator._controller.new_event( - id='INITIATE_USER_CONFIG', - key=network_function['id'], - desc_dict=event_desc) - orchestrator._controller.event_complete( - apply_config_event, result='FAILED') - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def handle_apply_user_config_basemode_exception( - orchestrator, event, exception): - - nfp_context = event.data - network_function = nfp_context['network_function'] - orchestrator.db_handler.update_network_function( - orchestrator.db_session, - network_function['id'], - {'status': nfp_constants.ERROR}) - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def handle_initiate_user_config_exception(orchestrator, event, exception): - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def handle_update_nf_description_exception(orchestrator, event, exception): - nfp_context = event.context - network_function = nfp_context['network_function'] - event_desc = nfp_context.pop('event_desc', None) - apply_config_event = orchestrator._controller.new_event( - id='INITIATE_USER_CONFIG', - key=network_function['id'], - desc_dict=event_desc) - orchestrator._controller.event_complete( - apply_config_event, result='FAILED') - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def handle_check_user_config_complete_exception( - orchestrator, event, exception): - - nfp_context = event.context - network_function = nfp_context['network_function'] - event_desc = nfp_context.pop('event_desc', None) - apply_config_event = orchestrator._controller.new_event( - id='INITIATE_USER_CONFIG', - key=network_function['id'], - desc_dict=event_desc) - orchestrator._controller.event_complete( - apply_config_event, result='FAILED') - orchestrator._controller.event_complete(event, result='FAILED') - return {'poll': False} - - @staticmethod - def handle_service_configured_exception(orchestrator, event, exception): - nfp_context = event.context - network_function = nfp_context['network_function'] - orchestrator.db_handler.update_network_function( - orchestrator.db_session, - network_function['id'], - {'status': nfp_constants.ERROR}) - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def handle_config_applied_exception(orchestrator, event, exception): - nfp_context = event.data['nfp_context'] - network_function = nfp_context['network_function'] - base_mode = nfp_context['base_mode'] - if base_mode: - orchestrator.db_handler.update_network_function( - orchestrator.db_session, - network_function['id'], - {'status': nfp_constants.ERROR}) - orchestrator._controller.event_complete(event, result='FAILED') - return - - event_desc = nfp_context['event_desc'] - key = nfp_context['key'] - id = nfp_context['id'] - ev = orchestrator._controller.new_event(id=id, key=key, - desc_dict=event_desc) - orchestrator._controller.event_complete(ev, result='FAILED') - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def handle_device_create_failed_exception(orchestrator, event, exception): - request_data = event.data - network_function_id = request_data['network_function_id'] - orchestrator.db_handler.update_network_function( - orchestrator.db_session, - network_function_id, - {'status': nfp_constants.ERROR}) - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def handle_delete_nfi_exception(orchestrator, event, exception): - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def handle_delete_user_config_exception(orchestrator, event, exception): - orchestrator._controller.event_complete(event, result='FAILED') - - @staticmethod - def handle_check_user_config_deleted_exception( - orchestrator, event, exception): - - request_data = event.data - nf_id = request_data['network_function_id'] - orchestrator._controller.event_complete(event, result='FAILED') - ducf_event = orchestrator._controller.new_event( - id='DELETE_USER_CONFIG', - key=nf_id, - binding_key=nf_id, - desc_dict=request_data['event_desc']) - orchestrator._controller.event_complete(ducf_event, result="FAILED") - return {"poll": False} - - @staticmethod - def handle_delete_network_function_db_exception( - orchestrator, event, exception): - - orchestrator._controller.event_complete(event, result='FAILED') diff --git a/gbpservice/nfp/orchestrator/openstack/openstack_driver.py b/gbpservice/nfp/orchestrator/openstack/openstack_driver.py deleted file mode 100644 index 0946a18d3..000000000 --- a/gbpservice/nfp/orchestrator/openstack/openstack_driver.py +++ /dev/null @@ -1,1500 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import io - -from gbpclient.v2_0 import client as gbp_client -from keystoneauth1.identity import v2 -from keystoneauth1.identity import v3 -from keystoneauth1 import session -from keystoneclient.v2_0 import client as identity_client -from keystoneclient.v3 import client as keyclientv3 -from neutronclient.v2_0 import client as neutron_client -from novaclient import client as nova_client -from novaclient import exceptions as nova_exc - -from gbpservice._i18n import _ -from gbpservice.nfp.core import log as nfp_logging -LOG = nfp_logging.getLogger(__name__) - - -class OpenstackApi(object): - """Initializes common attributes for openstack client drivers.""" - - def __init__(self, config, username=None, - password=None, tenant_name=None): - self.nova_version = '2' - self.config = config - self.identity_service = ("%s://%s:%d/%s/" % - (config.nfp_keystone_authtoken.auth_protocol, - config.nfp_keystone_authtoken.auth_host, - config.nfp_keystone_authtoken.auth_port, - config.nfp_keystone_authtoken.auth_version)) - self.network_service = ("%s://%s:%d/" % - (config.nfp_keystone_authtoken.auth_protocol, - config.nfp_keystone_authtoken.auth_host, - config.bind_port)) - self.username = username or config.nfp_keystone_authtoken.admin_user - self.password = password or ( - config.nfp_keystone_authtoken.admin_password) - self.tenant_name = (tenant_name or - config.nfp_keystone_authtoken.admin_tenant_name) - self.token = None - self.admin_tenant_id = None - - -class KeystoneClient(OpenstackApi): - """ Keystone Client Apis for orchestrator. """ - - def get_admin_token(self): - try: - admin_token = self.get_scoped_keystone_token( - self.config.nfp_keystone_authtoken.admin_user, - self.config.nfp_keystone_authtoken.admin_password, - self.config.nfp_keystone_authtoken.admin_tenant_name) - except Exception as ex: - err = ("Failed to obtain user token. Error: %s" % ex) - LOG.error(err) - raise Exception(err) - - return admin_token - - def get_scoped_keystone_token(self, user, password, tenant_name, - tenant_id=None): - """ Get a scoped token from Openstack Keystone service. - - A scoped token is bound to the specific tenant. - - :param user: User name - :param password: Password - :param tenantName: Tenant name - - :return: A scoped token or None if unable to get - """ - if not (tenant_name or tenant_id): - err = "Tenant Not specified for getting a scoped token" - LOG.error(err) - raise Exception(err) - try: - auth = v2.Password(username=user, - password=password, - tenant_name=tenant_name, - auth_url=self.identity_service) - sess = session.Session(auth=auth) - scoped_token = sess.get_token(auth=auth) - except Exception as err: - err = ("Failed to get token from" - " Openstack Keystone service" - " KeyError :: %s" % (err)) - self.config.nfp_keystone_authtoken.auth_port, - LOG.error(err) - raise Exception(err) - else: - return scoped_token - - def get_admin_tenant_id(self, token): - if not self.admin_tenant_id: - self.admin_tenant_id = self.get_tenant_id( - token, - self.config.nfp_keystone_authtoken.admin_tenant_name) - - return self.admin_tenant_id - - def get_tenant_id(self, token, tenant_name): - """ Get the tenant UUID associated to tenant name - - :param token: A scoped token - :param tenant: Tenant name - - :return: Tenant UUID - """ - try: - keystone = self._get_v2_keystone_admin_client() - tenant = keystone.tenants.find(name=tenant_name) - return tenant.id - except Exception as ex: - err = ("Failed to read tenant UUID from" - " tenant_name %s." - " Error :: %s" % (tenant_name, ex)) - LOG.error(err) - raise Exception(err) - err = 'No tenant with name "%s" found in keystone db' % tenant_name - LOG.error(err) - raise Exception(err) - - def _get_v2_keystone_admin_client(self): - """ Returns keystone v2 client with admin credentials - Using this client one can perform CRUD operations over - keystone resources. - """ - keystone_conf = self.config.nfp_keystone_authtoken - auth = v2.Password(username=keystone_conf.admin_user, - password=keystone_conf.admin_password, - tenant_name=keystone_conf.admin_tenant_name, - auth_url=self.identity_service) - sess = session.Session(auth=auth) - v2client = identity_client.Client(session=sess) - return v2client - - def _get_v3_keystone_admin_client(self): - """ Returns keystone v3 client with admin credentials - Using this client one can perform CRUD operations over - keystone resources. - """ - keystone_conf = self.config.nfp_keystone_authtoken - v3_auth_url = ('%s://%s:%s/%s/' % ( - keystone_conf.auth_protocol, keystone_conf.auth_host, - keystone_conf.auth_port, self.config.heat_driver.keystone_version)) - auth = v3.Password(auth_url=v3_auth_url, - user_domain_name='Default', - username=keystone_conf.admin_user, - password=keystone_conf.admin_password, - project_domain_name="Default", - project_name=keystone_conf.admin_tenant_name) - sess = session.Session(auth=auth) - v3client = keyclientv3.Client(session=sess) - return v3client - - -class NovaClient(OpenstackApi): - """ Nova Client Api driver. """ - - def get_image_id(self, token, tenant_id, image_name): - """ Get the image UUID associated to image name - - :param token: A scoped token - :param tenant_id: Tenant UUID - :param image_name: Image name - - :return: Image UUID - """ - try: - nova = nova_client.Client(self.nova_version, auth_token=token, - tenant_id=tenant_id, - auth_url=self.identity_service) - image = nova.images.find(name=image_name) - return image.id - except Exception as ex: - err = ("Failed to get image id from image name %s: %s" % ( - image_name, ex)) - LOG.error(err) - raise Exception(err) - - def get_image_metadata(self, token, tenant_id, image_name): - """ Get the image UUID associated to image name - - :param token: A scoped token - :param tenant_id: Tenant UUID - :param image_name: Image name - - :return: Image UUID - """ - try: - nova = nova_client.Client(self.nova_version, auth_token=token, - tenant_id=tenant_id, - auth_url=self.identity_service) - image = nova.images.find(name=image_name) - return image.metadata - except Exception as ex: - err = ("Failed to get image metadata from image name %s: %s" % ( - image_name, ex)) - LOG.error(err) - raise Exception(err) - - def get_flavor_id(self, token, tenant_id, flavor_name): - """ Get the flavor UUID associated to flavor name - - :param token: A scoped token - :param tenant_id: Tenant UUID - :param flavor_name: Flavor name - - :return: Flavor UUID or None if not found - """ - try: - nova = nova_client.Client(self.nova_version, auth_token=token, - tenant_id=tenant_id, - auth_url=self.identity_service) - flavor = nova.flavors.find(name=flavor_name) - return flavor.id - except Exception as ex: - err = ("Failed to get flavor id from flavor name %s: %s" % ( - flavor_name, ex)) - LOG.error(err) - raise Exception(err) - - def get_instance(self, token, tenant_id, instance_id): - """ Get instance details - - :param token: A scoped_token - :param tenant_id: Tenant UUID - :param instance_id: Instance UUID - - :return: Instance details - - """ - try: - nova = nova_client.Client(self.nova_version, auth_token=token, - tenant_id=tenant_id, - auth_url=self.identity_service) - instance = nova.servers.get(instance_id) - if instance: - return instance.to_dict() - raise Exception(_("No instance with id %(id)s " - "found in db for tenant %(tenant)s") - % {'id': instance_id, 'tenant': tenant_id}) - except Exception as ex: - err = ("Failed to read instance information from" - " Openstack Nova service's response" - " KeyError :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def get_keypair(self, token, tenant_id, keypair_name): - """ Get Nova keypair details - - :param token: A scoped_token - :param tenant_id: Tenant UUID - :param keypair_name: Nova keypair name - - :return: Nova keypair details - - """ - tenant_id = str(tenant_id) - try: - nova = nova_client.Client(self.nova_version, auth_token=token, - tenant_id=tenant_id, - auth_url=self.identity_service) - keypair = nova.keypairs.find(name=keypair_name) - return keypair.to_dict() - except Exception as ex: - err = ("Failed to read keypair information from" - " Openstack Nova service's response." - " %s" % ex) - LOG.error(err) - raise Exception(err) - - def attach_interface(self, token, tenant_id, instance_id, port_id): - """ Attaches a port to already created instance - :param token: A scoped token - :param tenant_id: Tenant UUID - :param instance_id: UUID of the instance - :param port_id: Port UUID - """ - try: - nova = nova_client.Client(self.nova_version, auth_token=token, - tenant_id=tenant_id, - auth_url=self.identity_service) - instance = nova.servers.interface_attach(instance_id, port_id, - None, None) - return instance - except Exception as ex: - err = ("Failed to attach interface %s to instance" - " %s %s" % (port_id, instance_id, ex)) - LOG.error(err) - raise Exception(err) - - def detach_interface(self, token, tenant_id, instance_id, port_id): - """ Detaches a port to already created instance - :param token: A scoped token - :param tenant_id: Tenant UUID - :param instance_id: UUID of the instance - :param port_id: Port UUID - """ - try: - nova = nova_client.Client(self.nova_version, auth_token=token, - tenant_id=tenant_id, - auth_url=self.identity_service) - instance = nova.servers.interface_detach(instance_id, port_id) - return instance - except Exception as ex: - err = ("Failed to detach interface %s from instance" - " %s %s" % (port_id, instance_id, ex)) - LOG.error(err) - raise Exception(err) - - def delete_instance(self, token, tenant_id, instance_id): - """ Delete the instance - - :param token: A scoped token - :param tenant_id: Tenant UUID - :param instance_id: Instance UUID - - """ - try: - nova = nova_client.Client(self.nova_version, auth_token=token, - tenant_id=tenant_id, - auth_url=self.identity_service) - nova.servers.delete(instance_id) - except Exception as ex: - err = ("Failed to delete instance" - " %s %s" % (instance_id, ex)) - LOG.error(err) - raise Exception(err) - - def get_instances(self, token, filters=None): - """ List instances - - :param token: A scoped_token - :param filters: Parameters for list filter - example for filter: {}, tenant_id is mandatory - - :return: instance List - - """ - if ( - not filters or - type(filters) != dict or - 'tenant_id' not in filters - ): - err = ("Failed to process get_instances," - " filters(type: dict) with tenant_id is mandatory") - LOG.error(err) - raise Exception(err) - - tenant_id = filters.get('tenant_id') - try: - nova = nova_client.Client(self.nova_version, auth_token=token, - tenant_id=tenant_id, - auth_url=self.identity_service) - instances = nova.servers.list(search_opts=filters) - data = [instance.to_dict() for instance in instances] - return data - except Exception as ex: - err = ("Failed to list instances under tenant" - " %s %s" % (tenant_id, ex)) - LOG.error(err) - raise Exception(err) - - def delete_affinity_group(self, token, tenant_id, nf_id): - """ Deletes a server group - - :param token: A scoped token - :param tenant_id: Tenant UUID - :param nf_id: Network Function UUID - - Returns: None - - """ - - nova_version = 2.15 - nova = nova_client.Client(nova_version, auth_token=token, - tenant_id=tenant_id, - auth_url=self.identity_service) - - try: - affinity_group = nova.server_groups.find(name=nf_id) - affinity_group_id = affinity_group.to_dict()['id'] - nova.server_groups.delete(affinity_group_id) - msg = ("Successfully deleted Nova Server Anti-Affinity " - "Group: %s" % nf_id) - LOG.info(msg) - except nova_exc.NotFound: - pass - except Exception as err: - msg = ("Failed to delete Nova Server Anti-Affinity Group " - "with name %s. Error: %s" % (nf_id, err)) - LOG.error(msg) - - def create_affinity_group(self, token, tenant_id, nf_id): - """ Creates a server group - - :param token: A scoped token - :param tenant_id: Tenant UUID - :param nf_id: Network Function UUID - - Returns: Nova server-group json object - - """ - - nova_version = 2.15 - kwargs = dict(name=nf_id, policies=['soft-anti-affinity']) - nova = nova_client.Client(nova_version, auth_token=token, - tenant_id=tenant_id, - auth_url=self.identity_service) - - try: - affinity_group = nova.server_groups.create(**kwargs) - affinity_group_id = affinity_group.to_dict()['id'] - msg = ("Successfully created Nova Server Anti-Affinity " - "Group: %s" % nf_id) - LOG.info(msg) - return affinity_group_id - except Exception as err: - msg = ("Failed to create Nova Server Anti-Affinity Group. " - "Error: %s" % err) - LOG.error(msg) - return None - - def create_instance(self, token, tenant_id, image_id, flavor, - nw_port_id_list, name, volume_support, - volume_size, secgroup_name=None, - metadata=None, files=None, config_drive=False, - userdata=None, key_name='', server_grp_id=None, - ): - """ Launch a VM with given details - - :param token: A scoped token - :param tenant_id: Tenant UUID - :param image_id: Image UUID - :param flavor: Flavor name - :param nw_port_id_list: Network UUID and port UUID list - :param name: Service istance name - :param secgroup_name: Nova security group name - :param metadata: metadata key-value pairs - :param files: List of files to be copied. - :example files: [{"dst": , - "src": }] - :param userdata: user data to pass to be exposed by the metadata - server this can be a file type object as well or a - string - :param key_name: Nova keypair name - :param server_grp_id: Nova server group UUID - :param volume_support: volume support to launch instance - :param volume_size: cinder volume size in GB - :return: VM instance UUID - - """ - try: - if files: - file_dict = {} - for _file in files: - with open(_file["src"]) as config_file: - data = config_file.read() - config_drive = True - file_dict.update({_file["dst"]: data}) - files = file_dict - except Exception as e: - msg = ( - "Failed while reading file: %r " % e) - LOG.error(msg) - raise e - - kwargs = dict() - if volume_support: - block_device_mapping_v2 = [ - { - "boot_index": "1", - "uuid": image_id, - "source_type": "image", - "volume_size": volume_size, - "destination_type": "volume", - "delete_on_termination": True - } - ] - kwargs.update(block_device_mapping_v2=block_device_mapping_v2) - - if server_grp_id: - kwargs.update(scheduler_hints={"group": server_grp_id}) - if key_name != '': - kwargs.update(key_name=key_name) - if config_drive is True: - kwargs.update(config_drive=True) - if userdata is not None and \ - (type(userdata) is str or type(userdata) is io.IOBase): - kwargs.update(userdata=userdata) - if metadata is not None and type(metadata) is dict and metadata != {}: - kwargs.update(meta=metadata) - if files is not None: - kwargs.update(files=files) - if nw_port_id_list: - nics = [{"port-id": entry.get("port"), "net-id": entry.get("uuid"), - "v4-fixed-ip": entry.get("fixed_ip")} - for entry in nw_port_id_list] - kwargs.update(nics=nics) - if secgroup_name: - kwargs.update(security_groups=[secgroup_name]) - - try: - nova = nova_client.Client(self.nova_version, auth_token=token, - tenant_id=tenant_id, - auth_url=self.identity_service) - flavor = nova.flavors.find(name=flavor) - instance = nova.servers.create(name, nova.images.get(image_id), - flavor, **kwargs) - data = instance.to_dict() - return data['id'] - except Exception as ex: - err = ("Failed to create instance under tenant" - " %s %s" % (tenant_id, ex)) - LOG.error(err) - raise Exception(err) - - -class NeutronClient(OpenstackApi): - """ Neutron Client Api Driver. """ - - def get_floating_ip(self, token, floatingip_id): - """ Get floatingip details - - :param token: A scoped_token - :param floatingip_id: Port UUID - - :return: floatingip details - - """ - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - return neutron.show_floatingip(floatingip_id)['floatingip'] - except Exception as ex: - err = ("Failed to read floatingip from" - " Openstack Neutron service's response" - " KeyError :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def get_floating_ips(self, token, **filters): - """ Get list of floatingips, associated with port if passed""" - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - return neutron.list_floatingips(**filters)['floatingips'] - except Exception as ex: - err = ("Failed to read floatingips from" - " Openstack Neutron service's response" - " KeyError :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def get_security_groups(self, token, tenant_id=None, filters=None): - """ Get list of security groups""" - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - filters = filters if filters is not None else {} - return neutron.list_security_groups(**filters)['security_groups'] - except Exception as ex: - err = ("Failed to get security groups from" - " Openstack Neutron service's response" - " KeyError :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def create_security_group(self, token, attrs=None): - """ Create security group""" - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - - sg_info = {"security_group": attrs} - return neutron.create_security_group(body=sg_info)[ - 'security_group'] - except Exception as ex: - err = ("Failed to get security groups from" - " Openstack Neutron service's response" - " KeyError :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def create_security_group_rule(self, token, attrs=None): - """ Create security group rule""" - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - - # attrs={'direction': 'egress', 'protocol': 'TCP', - # 'security_group_id': 'c90c7b29-f653-4c41-ae1a-0290dc64e020'} - sg_rule_info = {"security_group_rule": attrs} - return neutron.create_security_group_rule( - body=sg_rule_info)['security_group_rule'] - except Exception as ex: - err = ("Failed to get security groups from" - " Openstack Neutron service's response" - " KeyError :: %s" % (ex)) - LOG.error(err) - # raise Exception(err) - - def get_ports(self, token, filters=None): - """ List Ports - - :param token: A scoped_token - :param filters: Parameters for list filter - example for filter: ?tenant_id=%s&id=%s - - :return: Port List - - """ - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - ports = neutron.list_ports(**filters).get('ports', []) - return ports - except Exception as ex: - err = ("Failed to read port list from" - " Openstack Neutron service's response" - " KeyError :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def get_port(self, token, port_id): - """ Get port details - - :param token: A scoped_token - :param port_id: Port UUID - - :return: Port details - - """ - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - return neutron.show_port(port_id) - except Exception as ex: - err = ("Failed to read port information" - " Exception :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def get_subnets(self, token, filters=None): - """ List subnets - - :param token: A scoped_token - :param filters: Parameters for list filter - example for filter: ?tenant_id=%s&id=%s - - :return: Subnet List - - """ - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - subnets = neutron.list_subnets(**filters).get('subnets', []) - return subnets - except Exception as ex: - err = ("Failed to read subnet list from" - " Openstack Neutron service's response" - " KeyError :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def get_subnet(self, token, subnet_id): - """ Get subnet details - :param token: A scoped_token - :param subnet_id: Subnet UUID - :return: Subnet details - """ - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - return neutron.show_subnet(subnet_id) - except Exception as ex: - err = ("Failed to read subnet from" - " Openstack Neutron service's response" - " KeyError :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def delete_floatingip(self, token, floatingip_id): - """ Delete the floatingip - :param token: A scoped token - :param tenant_id: Tenant UUID - :param floatingip_id: Floatingip UUID - """ - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - return neutron.delete_floatingip(floatingip_id) - except Exception as ex: - err = ("Failed to delete floatingip from" - " Openstack Neutron service's response" - " KeyError :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def update_port(self, token, port_id, **kwargs): - """ - :param token: - :param port_id: - :param kwargs: name=<>, allowed_address_pairs={'ip_address': <>, - 'mac_address': <>} - :return: - """ - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - port_info = dict(port={}) - port_info['port'].update(kwargs) - return neutron.update_port(port_id, body=port_info) - except Exception as ex: - err = ("Failed to update port info" - " Error :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def get_floating_ips_for_ports(self, token, **kwargs): - """ - :param self: - :param token: - :param kwargs: - :return: - """ - data = {'floatingips': []} - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - data = neutron.list_floatingips(port_id=[kwargs[key] - for key in kwargs]) - return data - except Exception as ex: - raise Exception(ex) - - def _update_floatingip(self, token, floatingip_id, data): - """ Update the floatingip - :param token: A scoped token - :param floatingip_id: Floatingip UUID - :param data: data to update - """ - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - return neutron.update_floatingip(floatingip_id, body=data) - except Exception as ex: - err = ("Failed to update floatingip from" - " Openstack Neutron service's response" - " KeyError :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def disassociate_floating_ip(self, token, floatingip_id): - """ - :param self: - :param token: - :param floatingip_id: - :return: - """ - info = { - "floatingip": { - "port_id": None} - } - self._update_floatingip(token, floatingip_id, info) - LOG.debug("Successfully disassociated floatingip %s", - floatingip_id) - - def associate_floating_ip(self, token, floatingip_id, port_id): - """ - :param self: - :param token: - :param floatingip_id: - :return: - """ - - info = { - "floatingip": { - "port_id": port_id} - } - - self._update_floatingip(token, floatingip_id, info) - LOG.debug("Successfully associated floatingip %s", - floatingip_id) - - def list_ports(self, token, port_ids=None, **kwargs): - """ - :param token: - :param port_ids: - :param kwargs: - :return: - """ - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - port_ids = port_ids if port_ids is not None else [] - if port_ids: - ports = neutron.list_ports(id=port_ids).get('ports', []) - else: - ports = neutron.list_ports(**kwargs) - return ports - except Exception as ex: - err = ("Failed to list ports %s" % ex) - LOG.error(err) - raise Exception(err) - - def list_subnets(self, token, subnet_ids=None, **kwargs): - """ - :param token: - :param subnet_ids: - :param kwargs: - :return: - """ - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - subnet_ids = subnet_ids if subnet_ids is not None else [] - subnets = neutron.list_subnets(id=subnet_ids).get('subnets', []) - return subnets - except Exception as ex: - err = ("Failed to list subnets %s" % ex) - LOG.error(err) - raise Exception(err) - - def create_port(self, token, tenant_id, net_id, attrs=None): - - attr = { - 'port': { - # 'tenant_id': tenant_id, - 'network_id': net_id - } - } - - if attrs: - attr['port'].update(attrs) - - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - return neutron.create_port(body=attr)['port'] - except Exception as ex: - raise Exception(_("Port creation failed in network: %(net)r " - "of tenant: %(tenant)r Error: %(error)s") % - {'net': net_id, - 'tenant': tenant_id, - 'error': ex}) - - def delete_port(self, token, port_id): - """ - :param token: - :param port_id: - :return: - """ - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - return neutron.delete_port(port_id) - except Exception as ex: - err = ("Failed to delete port %s" - " Exception :: %s" % (port_id, ex)) - LOG.error(err) - raise Exception(err) - - def get_networks(self, token, filters=None): - """ List nets - - :param token: A scoped_token - :param filters: Parameters for list filter - example for filter: ?tenant_id=%s&id=%s - - :return: network List - - """ - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - nets = neutron.list_networks(**filters).get('networks', []) - return nets - except Exception as ex: - err = ("Failed to read network list from" - " Openstack Neutron service's response" - " KeyError :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def delete_nw(self, token, net_id): - """ - :param token: - :param net_id: - :return: - """ - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - return neutron.delete_network(net_id) - except Exception as ex: - err = ('Failed to delete network %s . %s' % (net_id, str(ex))) - LOG.error(err) - - def get_pools(self, token, filters=None): - """ List Pools - - :param token: A scoped_token - :param filters: Parameters for list filter - example for filter: ?tenant_id=%s&id=%s - - :return: Pool List - - """ - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - pools = neutron.list_pools(**filters).get('pools', []) - return pools - except Exception as ex: - err = ("Failed to read pool list from" - " Openstack Neutron service's response" - " KeyError :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def get_loadbalancers(self, token, filters=None): - """ List Loadbalancers - - :param token: A scoped_token - :param filters: Parameters for list filter - example for filter: ?tenant_id=%s&id=%s - - :return: Loadbalancers List - - """ - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - loadbalancers = neutron.list_loadbalancers(**filters).get( - 'loadbalancers', []) - return loadbalancers - except Exception as ex: - err = ("Failed to read pool list from" - " Openstack Neutron service's response" - " KeyError :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def get_vip(self, token, vip_id): - """ Get vip details - - :param token: A scoped_token - :param vip_id: Port UUID - - :return: VIP details - - """ - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - return neutron.show_vip(vip_id) - except Exception as ex: - err = ("Failed to read vip information" - " Exception :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def get_agents(self, token, filters=None): - """ Get neutron agents - - :param token: A scoped_token - :param filters: Parameters for list filter - - :return: neutron agents List - - """ - try: - neutron = neutron_client.Client(token=token, - endpoint_url=self.network_service) - return neutron.list_agents(**filters).get('agents', []) - except Exception as ex: - err = ("Failed to read agents information" - " Exception :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - -class GBPClient(OpenstackApi): - """ GBP Client Api Driver. """ - - def get_policy_target_groups(self, token, filters=None): - """ List Policy Target Groups - - :param token: A scoped_token - :param filters: Parameters for list filter - example for filter: ?tenant_id=%s&id=%s - - :return: PTG List - - """ - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - return gbp.list_policy_target_groups( - **filters)['policy_target_groups'] - except Exception as ex: - err = ("Failed to read PTG list from" - " Openstack Neutron service's response." - " KeyError :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def get_policy_target_group(self, token, ptg_id, filters=None): - """ - :param token: A scoped token - :param ptg_id: PTG - :param filters: Optional - :return: - """ - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - filters = filters if filters is not None else {} - return gbp.show_policy_target_group( - ptg_id, **filters)['policy_target_group'] - except Exception as ex: - err = ("Failed to read PTG list from" - " Openstack Neutron service's response." - " KeyError :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def update_policy_target_group(self, token, ptg_id, - policy_target_group_info): - """ Updates a GBP Policy Target Group - - :param token: A scoped token - :param ptg_id: PTG UUID - :param policy_target_group_info: PTG info dict - :return: PTG dict - """ - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - return gbp.update_policy_target_group( - ptg_id, - body=policy_target_group_info)['policy_target_group'] - except Exception as ex: - err = ("Failed to update policy target group. Error :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def create_policy_target(self, token, tenant_id, - policy_target_group_id, name, port_id=None, - description=''): - """ Creates a GBP Policy Target - - :param token: A scoped token - :param tenant_id: Tenant UUID - :param policy_target_group_id: PTG UUID - :param name: PT name - :return: PT dict - """ - policy_target_info = { - "policy_target": { - "policy_target_group_id": policy_target_group_id, - "tenant_id": tenant_id, - } - } - if name: - policy_target_info['policy_target'].update({'name': name}) - if port_id: - policy_target_info["policy_target"]["port_id"] = port_id - if description: - policy_target_info["policy_target"]["description"] = description - - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - return gbp.create_policy_target( - body=policy_target_info)['policy_target'] - - except Exception as ex: - err = ("Failed to read policy target information from" - " Openstack Neutron service's response." - " KeyError :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def delete_policy_target(self, token, policy_target_id): - """ Delete the GBP policy_target - :param token: A scoped token - :param policy_target_id: PT UUID - """ - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - return gbp.delete_policy_target(policy_target_id) - - except Exception as ex: - err = ("Failed to delete policy target information from" - " Openstack Neutron service's response." - " KeyError :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def delete_policy_target_group(self, token, policy_target_group_id): - """ Delete the GBP policy_target group - :param token: A scoped token - :param policy_target_id: PTG UUID - """ - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - return gbp.delete_policy_target_group(policy_target_group_id) - except Exception as ex: - err = ("Failed to delete policy target group from" - " Openstack." - " Error :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def update_policy_target(self, token, policy_target_id, updated_pt): - """ Update the Policy Target - :param token: A scoped token - :param policy_target_id: PT UUID - :param updated_pt: New PT dict - {\"policy_target\": {\"description\": \"test123\"}} - """ - - policy_target_info = { - "policy_target": updated_pt - } - - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - return gbp.update_policy_target( - policy_target_id, body=policy_target_info)['policy_target'] - except Exception as ex: - err = ("Failed to read updated PT information" - ". PT %s." - " KeyError :: %s" % (policy_target_id, ex)) - LOG.error(err) - raise Exception(err) - - def create_policy_target_group(self, token, tenant_id, name, - l2_policy_id=None, ext_data=None): - """ Creates a GBP Policy Target Group - - :param ext_data: - :param token: A scoped token - :param tenant_id: Tenant UUID - :param name: PTG name - :return: PTG dict - """ - - policy_target_group_info = { - "policy_target_group": { - "tenant_id": tenant_id, - "name": name, - } - } - ext_data = ext_data or {} - if l2_policy_id: - policy_target_group_info["policy_target_group"].update( - {"l2_policy_id": l2_policy_id}) - policy_target_group_info['policy_target_group'].update(ext_data) - - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - return gbp.create_policy_target_group( - body=policy_target_group_info)['policy_target_group'] - except Exception as ex: - err = ("Failed to create policy target group. %s" - " Error :: %s" % (policy_target_group_info, ex)) - LOG.error(err) - raise Exception(err) - - def create_l2_policy(self, token, tenant_id, name, l3_policy_id=None, - description=''): - - l2_policy_info = { - "l2_policy": { - "tenant_id": tenant_id, - "name": name - } - } - if l3_policy_id: - l2_policy_info["l2_policy"].update({'l3_policy_id': l3_policy_id}) - if description: - l2_policy_info["description"].update({'description': description}) - - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - return gbp.create_l2_policy(body=l2_policy_info)['l2_policy'] - except Exception as ex: - err = ("Failed to create l2 policy under tenant" - " %s. Error :: %s" % (tenant_id, ex)) - LOG.error(err) - raise Exception(err) - - def delete_l2_policy(self, token, l2policy_id): - """ - :param token: - :param l2policy_id: - :return: - """ - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - return gbp.delete_l2_policy(l2policy_id) - except Exception as ex: - err = ("Failed to delete l2 policy %s. Reason %s" % - (l2policy_id, ex)) - LOG.error(err) - raise Exception(err) - - # NOTE: The plural form in the function name is needed in that way - # to construct the function generically - def get_l2_policys(self, token, filters=None): - """ List L2 policies - - :param token: A scoped_token - :param filters: Parameters for list filter - example for filter: {} - - :return: L2 policies List - - """ - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - filters = filters if filters is not None else {} - return gbp.list_l2_policies(**filters)['l2_policies'] - except Exception as ex: - err = ("Failed to list l2 policies. Reason %s" % ex) - LOG.error(err) - raise Exception(err) - - def get_l2_policy(self, token, policy_id, filters=None): - """ List L2 policies - - :param token: A scoped_token - :param policy_id: l2 policy id - :param filters: Parameters for list filter - example for filter: {} - - :return: L2 policies List - - """ - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - filters = filters if filters is not None else {} - return gbp.show_l2_policy( - policy_id, **filters)['l2_policy'] - except Exception as ex: - err = ("Failed to read l2 policy list from" - " Openstack Neutron service's response." - " KeyError :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def create_network_service_policy(self, token, - network_service_policy_info): - - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - return gbp.create_network_service_policy( - body=network_service_policy_info)['network_service_policy'] - except Exception as ex: - err = ("Failed to create network service policy " - "Error :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def get_network_service_policies(self, token, filters=None): - """ List network service policies - - :param token: A scoped_token - :param filters: Parameters for list filter - example for filter: {} - - :return: network service policy List - - """ - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - filters = filters if filters is not None else {} - return gbp.list_network_service_policies(**filters)[ - 'network_service_policies'] - except Exception as ex: - err = ("Failed to list network service policies. Reason %s" % ex) - LOG.error(err) - raise Exception(err) - - def get_external_policies(self, token, filters=None): - """ List external policies - - :param token: A scoped_token - :param filters: Parameters for list filter - example for filter: {} - - :return: external policy List - - """ - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - filters = filters if filters is not None else {} - return gbp.list_external_policies(**filters)['external_policies'] - except Exception as ex: - err = ("Failed to list external policies. Reason %s" % ex) - LOG.error(err) - raise Exception(err) - - def get_policy_rule_sets(self, token, filters=None): - """ List policy rule sets - - :param token: A scoped_token - :param filters: Parameters for list filter - example for filter: {} - - :return: policy rule set List - - """ - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - filters = filters if filters is not None else {} - return gbp.list_policy_rule_sets(**filters)['policy_rule_sets'] - except Exception as ex: - err = ("Failed to list policy rule sets. Reason %s" % ex) - LOG.error(err) - raise Exception(err) - - def get_policy_actions(self, token, filters=None): - """ List policy actions - - :param token: A scoped_token - :param filters: Parameters for list filter - example for filter: {} - - :return: policy actions List - - """ - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - filters = filters if filters is not None else {} - return gbp.list_policy_actions(**filters)['policy_actions'] - except Exception as ex: - err = ("Failed to list policy actions. Reason %s" % ex) - LOG.error(err) - raise Exception(err) - - def get_policy_rules(self, token, filters=None): - """ List policy rules - - :param token: A scoped_token - :param filters: Parameters for list filter - example for filter: {} - - :return: policy rules List - - """ - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - filters = filters if filters is not None else {} - return gbp.list_policy_rules(**filters)['policy_rules'] - except Exception as ex: - err = ("Failed to list policy rules. Reason %s" % ex) - LOG.error(err) - raise Exception(err) - - def create_l3_policy(self, token, l3_policy_info): # tenant_id, name): - - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - return gbp.create_l3_policy(body=l3_policy_info)['l3_policy'] - except Exception as ex: - err = ("Failed to create l3 policy under tenant" - " %s. Error :: %s" - % (l3_policy_info['l3_policy']['tenant_id'], ex)) - LOG.error(err) - raise Exception(err) - - def get_l3_policy(self, token, policy_id, filters=None): - """ List L3 policies - - :param token: A scoped_token - :param filters: Parameters for list filter - example for filter: {} - - :return: L3 policies List - - """ - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - filters = filters if filters is not None else {} - return gbp.show_l3_policy( - policy_id, **filters)['l3_policy'] - except Exception as ex: - err = ("Failed to read l3 policy list from" - " Openstack Neutron service's response." - " KeyError :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def get_l3_policies(self, token, filters=None): - """ List L3 policies - - :param token: A scoped_token - :param filters: Parameters for list filter - example for filter: {} - - :return: L2 policies List - - """ - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - filters = filters if filters is not None else {} - return gbp.list_l3_policies(**filters)['l3_policies'] - except Exception as ex: - err = ("Failed to list l3 policies. Reason %s" % ex) - LOG.error(err) - raise Exception(err) - - def get_policy_targets(self, token, filters=None): - """ List Policy Targets - - :param token: A scoped_token - :param filters: Parameters for list filter - example for filter: {} - - :return: PT List - - """ - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - filters = filters if filters is not None else {} - return gbp.list_policy_targets(**filters)['policy_targets'] - except Exception as ex: - err = ("Failed to read PT list." - " Error :: %s" % (ex)) - LOG.error(err) - raise Exception(err) - - def list_pt(self, token, filters=None): - filters = filters if filters is not None else {} - return self.get_policy_targets(token, filters=filters) - - def get_policy_target(self, token, pt_id, filters=None): - try: - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - filters = filters if filters is not None else {} - return gbp.show_policy_target(pt_id, - **filters)['policy_target'] - except Exception as ex: - err = ("Failed to read PT information" - ". PT %s." - " Error :: %s" % (pt_id, ex)) - LOG.error(err) - raise Exception(err) - - def get_service_profile(self, token, service_profile_id): - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - return gbp.show_service_profile(service_profile_id)['service_profile'] - - def get_servicechain_node(self, token, node_id): - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - return gbp.show_servicechain_node(node_id)['servicechain_node'] - - def get_servicechain_instance(self, token, instance_id): - gbp = gbp_client.Client(token=token, - endpoint_url=self.network_service) - return gbp.show_servicechain_instance(instance_id)[ - 'servicechain_instance'] diff --git a/gbpservice/nfp/pecan/api/config.py b/gbpservice/nfp/pecan/api/config.py deleted file mode 100644 index dc7cdb23d..000000000 --- a/gbpservice/nfp/pecan/api/config.py +++ /dev/null @@ -1,78 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -# Server Specific Configurations -server = { - 'port': '8080', - 'host': '0.0.0.0' -} - -# Pecan Application Configurations -app = { - 'root': 'root_controller.RootController', - 'modules': ['v1'], - 'debug': True, - 'errors': { - 404: '/error/404', - '__force_dict__': True - } -} - -logging = { - 'root': {'level': 'INFO', 'handlers': ['console', 'logfile']}, - 'loggers': { - 'pecanlog': {'level': 'INFO', - 'handlers': ['console', 'logfile'], - 'propagate': False}, - 'pecan': {'level': 'INFO', - 'handlers': ['console', 'logfile'], - 'propagate': False}, - 'py.warnings': {'handlers': ['console', 'logfile']}, - '__force_dict__': True - }, - 'handlers': { - 'console': { - 'level': 'INFO', - 'class': 'logging.StreamHandler', - 'formatter': 'color' - }, - 'logfile': { - 'class': 'logging.FileHandler', - 'filename': '/var/log/nfp/nfp_pecan.log', - 'level': 'INFO' - } - }, - 'formatters': { - 'simple': { - 'format': ('%(asctime)s %(levelname)-5.5s [%(name)s]' - '[%(threadName)s] %(message)s') - }, - 'color': { - '()': 'pecan.log.ColorFormatter', - 'format': ('%(asctime)s [%(padded_color_levelname)s] [%(name)s]' - '[%(threadName)s] %(message)s'), - '__force_dict__': True - } - } -} - -cloud_services = [ - {'service_name': 'configurator', - 'topic': 'configurator', # configurator rpc topic - 'reporting_interval': '10', # in seconds - 'apis': ['CONFIGURATION'], - # notifications from configurator to UTC components - 'notifications': {'host': '127.0.0.1', - 'queue': 'configurator-notifications' - } - }, -] diff --git a/gbpservice/nfp/pecan/api/configurator_decider.py b/gbpservice/nfp/pecan/api/configurator_decider.py deleted file mode 100644 index b97c52c38..000000000 --- a/gbpservice/nfp/pecan/api/configurator_decider.py +++ /dev/null @@ -1,30 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import pecan - -from gbpservice.nfp.pecan import constants - - -class DecideConfigurator(pecan.commands.serve.ServeCommand): - ''' decides the type of configurtor to be used - like base_configurator or reference_configurator - ''' - arguments = pecan.commands.serve.ServeCommand.arguments + ({ - 'name': '--mode', - 'help': 'decides the type of configurtor to be used', - 'choices': constants.modes, - },) - - def run(self, args): - setattr(pecan, 'mode', args.mode) - super(DecideConfigurator, self).run(args) diff --git a/gbpservice/nfp/pecan/api/root_controller.py b/gbpservice/nfp/pecan/api/root_controller.py deleted file mode 100644 index 88940b5e0..000000000 --- a/gbpservice/nfp/pecan/api/root_controller.py +++ /dev/null @@ -1,33 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import pecan - -from gbpservice.nfp.pecan.constants import controller_mode_map -from gbpservice.nfp.pecan.constants import controllers - - -class RootController(object): - """This is root controller that forward the request to __init__.py - file inside controller folder inside v1 - - """ - - controller = __import__(controllers[controller_mode_map[pecan.mode]], - globals(), locals(), ['controllers'], -1) - v1 = controller.V1Controller() - - @pecan.expose() - def get(self): - return {'versions': [{'status': 'CURRENT', - 'updated': '2014-12-11T00:00:00Z', - 'id': 'v1'}]} diff --git a/gbpservice/nfp/pecan/api/setup.py b/gbpservice/nfp/pecan/api/setup.py deleted file mode 100644 index 1e84d2d96..000000000 --- a/gbpservice/nfp/pecan/api/setup.py +++ /dev/null @@ -1,39 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -try: - import setuptools -except ImportError: - import ez_setup - ez_setup.use_setuptools() - import setuptools - -setuptools.setup( - name='api', - version='0.1', - description='', - author='', - author_email='', - install_requires=[ - "pecan", - ], - test_suite='api', - zip_safe=False, - include_package_data=True, - packages=setuptools.find_packages(exclude=['ez_setup']), - # Having entry point gives the option to define custom classes - # to improve the flexibility in accessing different configurators - entry_points=""" - [pecan.command] - configurator_decider = configurator_decider:DecideConfigurator - """ -) diff --git a/gbpservice/nfp/pecan/api/v1/app.py b/gbpservice/nfp/pecan/api/v1/app.py deleted file mode 100644 index 3dcb5ea23..000000000 --- a/gbpservice/nfp/pecan/api/v1/app.py +++ /dev/null @@ -1,23 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import pecan - - -def setup_app(config): - - app_conf = dict(config.app) - return pecan.make_app( - app_conf.pop('root'), - logging=getattr(config, 'logging', {}), - **app_conf - ) diff --git a/gbpservice/nfp/pecan/base_controller.py b/gbpservice/nfp/pecan/base_controller.py deleted file mode 100644 index 9499922f2..000000000 --- a/gbpservice/nfp/pecan/base_controller.py +++ /dev/null @@ -1,50 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import zlib - -from oslo_log import log as logging -import oslo_serialization.jsonutils as jsonutils - -from pecan.hooks import HookController -from pecan.hooks import PecanHook -from pecan import rest - -LOG = logging.getLogger(__name__) - - -class ZipperHook(PecanHook): - - def before(self, state): - if state.request.method.upper() != 'GET': - try: - zippedBody = state.request.body - body = zlib.decompress(zippedBody) - body = jsonutils.loads(body) - state.request.json_body = body - state.request.content_type = "application/json" - except Exception as e: - msg = ("Failed to process data ,Reason: %s" % (e)) - LOG.error(msg) - - def after(self, state): - data = state.response.body - state.response.body = zlib.compress(data) - state.response.content_type = "application/octet-stream" - - -class BaseController(rest.RestController, HookController): - """This is root controller that forward the request to __init__.py - file inside controller folder inside v1 - - """ - __hooks__ = [ZipperHook()] diff --git a/gbpservice/nfp/pecan/constants.py b/gbpservice/nfp/pecan/constants.py deleted file mode 100644 index bc6ea9378..000000000 --- a/gbpservice/nfp/pecan/constants.py +++ /dev/null @@ -1,35 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - - -BASE_CONTROLLER = 'base_controller' -REFERENCE_CONTROLLER = 'reference_controller' -ADVANCED_CONTROLLER = 'advanced_controller' - -base_with_vm = 'base_with_vm' -base = 'base' -advanced = 'advanced' -modes = [base, base_with_vm, advanced] - -controller_mode_map = { - base: BASE_CONTROLLER, - base_with_vm: REFERENCE_CONTROLLER, - advanced: ADVANCED_CONTROLLER -} - -controllers = { - controller_mode_map[base]: 'gbpservice.nfp.base_configurator.controllers', - controller_mode_map[base_with_vm]: ('gbpservice.contrib' - '.nfp_service.reference_configurator.controllers'), - controller_mode_map[advanced]: ('gbpservice.contrib.nfp.configurator' - '.advanced_controller.controller_loader') -} diff --git a/gbpservice/nfp/proxy_agent/lib/topics.py b/gbpservice/nfp/proxy_agent/lib/topics.py deleted file mode 100644 index 82903ae05..000000000 --- a/gbpservice/nfp/proxy_agent/lib/topics.py +++ /dev/null @@ -1,16 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -PROXY_AGENT_TOPIC = 'nfp-proxy-agent-topic' -SERVICE_ORCH_TOPIC = 'nfp-nso-notification-topic' -DEVICE_ORCH_TOPIC = 'nfp-ndo-notification-topic' -CONFIG_ORCH_TOPIC = 'nfp-nco-notification-topic' diff --git a/gbpservice/nfp/proxy_agent/modules/notification_agent.py b/gbpservice/nfp/proxy_agent/modules/notification_agent.py deleted file mode 100644 index 2e8225e68..000000000 --- a/gbpservice/nfp/proxy_agent/modules/notification_agent.py +++ /dev/null @@ -1,34 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from gbpservice.nfp.core.event import Event -from gbpservice.nfp.proxy_agent.notifications import pull - - -def events_init(sc, conf): - """Register event with its handler.""" - evs = [ - Event(id='PULL_NOTIFICATIONS', - handler=pull.PullNotification(sc, conf))] - sc.register_events(evs) - - -def nfp_module_init(sc, conf): - """Initialize module to register rpc & event handler""" - events_init(sc, conf) - - -def nfp_module_post_init(sc, conf): - """Post a event for pull notification after each periodic_task_interval""" - ev = sc.new_event(id='PULL_NOTIFICATIONS', - key='PULL_NOTIFICATIONS') - sc.post_event(ev) diff --git a/gbpservice/nfp/proxy_agent/modules/proxy_agent.py b/gbpservice/nfp/proxy_agent/modules/proxy_agent.py deleted file mode 100644 index eed0ba34d..000000000 --- a/gbpservice/nfp/proxy_agent/modules/proxy_agent.py +++ /dev/null @@ -1,112 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from gbpservice.nfp.core import context as module_context -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.core.rpc import RpcAgent -from gbpservice.nfp.lib import transport as transport -from gbpservice.nfp.proxy_agent.lib import topics - - -from oslo_log import helpers as log_helpers -import oslo_messaging as messaging - -LOG = nfp_logging.getLogger(__name__) - - -def rpc_init(config, sc): - """Register agent with its handler.""" - rpcmgr = RpcHandler(config, sc) - agent = RpcAgent( - sc, - host=config.host, - topic=topics.PROXY_AGENT_TOPIC, - manager=rpcmgr) - sc.register_rpc_agents([agent]) - - -def nfp_module_init(sc, conf): - """Initialize module to register rpc & event handler""" - rpc_init(conf, sc) - - -class RpcHandler(object): - RPC_API_VERSION = '1.0' - target = messaging.Target(version=RPC_API_VERSION) - - def __init__(self, conf, sc): - super(RpcHandler, self).__init__() - self._conf = conf - self._sc = sc - - @log_helpers.log_method_call - def create_network_function_config(self, context, body): - """Method of rpc handler for create_network_function_config. - Return: Http Response. - """ - module_context.init() - transport.send_request_to_configurator(self._conf, - context, body, - "CREATE") - - @log_helpers.log_method_call - def delete_network_function_config(self, context, body): - """Method of rpc handler for delete_network_function_config. - Return: Http Response. - """ - module_context.init() - transport.send_request_to_configurator(self._conf, - context, body, - "DELETE") - - @log_helpers.log_method_call - def update_network_function_config(self, context, body): - """Method of rpc handler for delete_network_function_config. - Return: Http Response. - """ - module_context.init() - transport.send_request_to_configurator(self._conf, - context, body, - "UPDATE") - - @log_helpers.log_method_call - def create_network_function_device_config(self, context, body): - """Method of rpc handler for create_network_function_device_config. - Return: Http Response. - """ - module_context.init() - transport.send_request_to_configurator(self._conf, - context, body, - "CREATE", - device_config=True) - - @log_helpers.log_method_call - def delete_network_function_device_config(self, context, body): - """Method of rpc handler for delete_network_function_device_config. - Return: Http Response. - """ - module_context.init() - transport.send_request_to_configurator(self._conf, - context, body, - "DELETE", - device_config=True) - - @log_helpers.log_method_call - def network_function_event(self, context, body): - """Method of rpc handler for create_service. - Return: Http Response. - """ - module_context.init() - transport.send_request_to_configurator(self._conf, - context, body, - "CREATE", - network_function_event=True) diff --git a/gbpservice/nfp/proxy_agent/notifications/pull.py b/gbpservice/nfp/proxy_agent/notifications/pull.py deleted file mode 100644 index 5ee4c192b..000000000 --- a/gbpservice/nfp/proxy_agent/notifications/pull.py +++ /dev/null @@ -1,97 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import sys -import traceback - -from neutron_lib import context as n_context - -from gbpservice.nfp.common import constants as nfp_constants -from gbpservice.nfp.core import log as nfp_logging -from gbpservice.nfp.core import module as nfp_api -from gbpservice.nfp.lib import transport as transport -from gbpservice.nfp.proxy_agent.lib import topics as a_topics - -LOG = nfp_logging.getLogger(__name__) - -ResourceMap = { - 'device_orch': a_topics.DEVICE_ORCH_TOPIC, - 'service_orch': a_topics.SERVICE_ORCH_TOPIC, - 'nas_service': a_topics.CONFIG_ORCH_TOPIC -} - - -"""Periodic Class to pull notification from configurator""" - - -class PullNotification(nfp_api.NfpEventHandler): - - def __init__(self, sc, conf): - self._sc = sc - self._conf = conf - - def handle_event(self, ev): - self._sc.poll_event(ev) - - def _method_handler(self, notification): - # Method handles notification as per resource, resource_type and method - try: - requester = notification['info']['context']['requester'] - if requester in ResourceMap: - topic = ResourceMap[requester] - context = notification['info']['context']['neutron_context'] - rpc_ctx = n_context.Context.from_dict(context) - else: - topic = requester.lower() + '_notifications' - rpc_ctx = n_context.get_admin_context() - rpcClient = transport.RPCClient(topic) - rpcClient.cctxt.cast(rpc_ctx, - 'network_function_notification', - notification_data=notification) - except Exception as e: - msg = "Exception while calling notification: %s" % (e) - LOG.error(msg) - raise Exception(e) - - @nfp_api.poll_event_desc(event='PULL_NOTIFICATIONS', - spacing=nfp_constants.PULL_NOTIFICATIONS_SPACING) - def pull_notifications(self, ev): - """Pull and handle notification from configurator.""" - notifications = transport.get_response_from_configurator(self._conf) - - if not isinstance(notifications, list): - message = "Notfications not list, %s" % (notifications) - LOG.error(message) - - else: - for notification in notifications: - if not notification: - message = "Receiver Response: Empty" - LOG.info(message) - continue - try: - self._method_handler(notification) - except AttributeError: - exc_type, exc_value, exc_traceback = sys.exc_info() - message = ("AttributeError while handling" - "message %s : %s " % (notification, - traceback.format_exception( - exc_type, exc_value, exc_traceback))) - LOG.error(message) - - except Exception as e: - exc_type, exc_value, exc_traceback = sys.exc_info() - message = ("Generic exception (%s)" - "while handling message (%s) : %s" % ( - e, notification, traceback.format_exception( - exc_type, exc_value, exc_traceback))) - LOG.error(message) diff --git a/gbpservice/nfp/proxy_agent/proxy/proxy.py b/gbpservice/nfp/proxy_agent/proxy/proxy.py deleted file mode 100644 index c60483682..000000000 --- a/gbpservice/nfp/proxy_agent/proxy/proxy.py +++ /dev/null @@ -1,375 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import os -import socket -import sys -import time - -import eventlet -eventlet.monkey_patch() - -from oslo_config import cfg as oslo_config -from oslo_log import log as oslo_logging - -from gbpservice._i18n import _ -from gbpservice.nfp.core import log as nfp_logging - - -oslo_logging.register_options(oslo_config.CONF) - -if not hasattr(oslo_config.CONF, 'module'): - module_opts = [ - oslo_config.StrOpt('module', - default='proxy', - help='component name for logging.')] - oslo_config.CONF.register_opts(module_opts) - -LOG = nfp_logging.getLogger(__name__) - -# Queue of proxy connections which workers will handle -ConnQ = eventlet.queue.Queue(maxsize=0) - -tcp_open_connection_count = 0 -tcp_close_connection_count = 0 - - -class ConnectionIdleTimeOut(Exception): - - ''' - Exception raised when connection is idle for configured timeout - ''' - pass - - -""" -parsing the proxy configuration file -""" - - -class Configuration(object): - - def __init__(self, conf): - self.unix_bind_path = conf.proxy.unix_bind_path - self.max_connections = conf.proxy.max_connections - self.worker_threads = conf.proxy.worker_threads - self.connect_max_wait_timeout = conf.proxy.connect_max_wait_timeout - self.idle_max_wait_timeout = conf.proxy.idle_max_wait_timeout - self.idle_min_wait_timeout = conf.proxy.idle_min_wait_timeout - self.rest_server_address = conf.proxy.nfp_controller_ip - self.rest_server_port = conf.proxy.nfp_controller_port - - -""" -Class to create Unix Listener -""" - - -class UnixServer(object): - - def __init__(self, conf, proxy): - self.proxy = proxy - self.bind_path = conf.unix_bind_path - self.max_connections = conf.max_connections - # Make sure the socket does not already exist - try: - os.unlink(self.bind_path) - except OSError: - if os.path.exists(self.bind_path): - raise - - # Create a UDS socket - self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - - # Bind the socket to the port - message = 'starting up on %s' % self.bind_path - LOG.info(message) - self.socket.bind(self.bind_path) - self.socket.listen(self.max_connections) - - def listen(self): - client, address = self.socket.accept() - self.proxy.new_client(client, address) - - -""" -Class to create TCP client Connection if -TCP server is alive -""" - - -class TcpClient(object): - - def __init__(self, conf, proxy): - self.conf = conf - self.proxy = proxy - self.server_address = conf.rest_server_address - self.server_port = conf.rest_server_port - # Connect the socket to the port where the server is listening - self.server = (self.server_address, self.server_port) - - def connect(self): - sock = socket.socket() - message = 'connecting to %s port %s' % self.server - LOG.info(message) - sock.settimeout(self.conf.connect_max_wait_timeout) - try: - sock.connect(self.server) - except socket.error as exc: - message = "Caught exception socket.error : %s" % exc - LOG.error(message) - return sock, False - return sock, True - - -""" -ADT for proxy connection -""" - - -class Connection(object): - - def __init__(self, conf, socket, type='unix'): - self._socket = socket - self._idle_wait = conf.idle_min_wait_timeout - self._idle_timeout = conf.idle_max_wait_timeout - self._idle_count_max = (self._idle_timeout / self._idle_wait) - self._idle_count = 0 - self._start_time = time.time() - self._end_time = time.time() - self.type = type - self.socket_id = self._socket.fileno() - - def _tick(self): - self._idle_count += 1 - - def _timedout(self): - if self._idle_count > self._idle_count_max: - self._end_time = time.time() - raise ConnectionIdleTimeOut( - _("Connection (%(conn)d) - " - "stime (%(start_time)s) - etime (%(end_time)s) - " - "idle_count (%(idle)d) idle_count_max(%(idle_max)d)") % - {'conn': self.identify(), - 'start_time': self._start_time, - 'end_time': self._end_time, - 'idle': self._idle_count, - 'idle_max': self._idle_count_max}) - - def idle(self): - self._tick() - self._timedout() - - def idle_reset(self): - self._idle_count = 0 - self._start_time = time.time() - - def _wait(self, timeout): - if self.type == 'unix': - eventlet.sleep(timeout) - self._socket.setblocking(0) - else: - self._socket.settimeout(timeout) - - def recv(self): - self._wait(self._idle_wait) - try: - data = self._socket.recv(1024) - if data and len(data): - self.idle_reset() - return data - self.idle() - except socket.timeout: - self.idle() - except socket.error: - self.idle() - return None - - def send(self, data): - self._socket.setblocking(1) - self._socket.sendall(data) - self._socket.setblocking(0) - - def close(self): - message = "Closing Socket - %d" % (self.identify()) - LOG.debug(message) - try: - self._socket.shutdown(socket.SHUT_RDWR) - self._socket.close() - except Exception as exc: - message = "%s - exception while closing - %s" % ( - self.identify(), str(exc)) - LOG.error(message) - - def identify(self): - return self.socket_id - - -""" -ADT for Proxy Connection Object -Each Connection Object is pair of Unix Socket and -TCP Client Socket -""" - - -class ProxyConnection(object): - - def __init__(self, conf, unix_socket, tcp_socket): - self._unix_conn = Connection(conf, unix_socket, type='unix') - self._tcp_conn = Connection(conf, tcp_socket, type='tcp') - message = "New Proxy - Unix - %d, TCP - %d" % ( - self._unix_conn.identify(), self._tcp_conn.identify()) - LOG.debug(message) - - def close(self): - self._unix_conn.close() - self._tcp_conn.close() - - def _proxy(self, rxconn, txconn): - data = rxconn.recv() - if data: - txconn.send(data) - - def run(self): - try: - self._proxy(self._unix_conn, self._tcp_conn) - self._proxy(self._tcp_conn, self._unix_conn) - return True - except Exception as exc: - message = "%s" % (exc) - LOG.debug(message) - self._unix_conn.close() - self._tcp_conn.close() - return False - - def identify(self): - return '%d:%d' % ( - self._unix_conn.identify(), - self._tcp_conn.identify()) - - -""" -ADT for proxy Worker -""" - - -class Worker(object): - - def run(self): - """ - Worker thread will pop the Proxy Connection Object - from Connection Queue and Perform send and receive - operations. If the connection is ideal upto ideal_max_timeout - it will not push the Object into connection queue so Proxy Connection - Object is automatically destroy, otherwise it will again - push the Object in connection Queue - """ - while True: - try: - pc = ConnQ.get() - call = True - while call: - call = pc.run() - except eventlet.queue.Empty: - pass - eventlet.sleep(0) - - -""" -ADT to Run the configurator proxy, - accept the Unix Client request, - Check REST Server is reachable or not, - Try to establish TCP Client Connection to REST - -""" - - -class Proxy(object): - - def __init__(self, conf): - self.conf = conf - # Be a server and wait for connections from the client - self.server = UnixServer(conf, self) - self.client = TcpClient(conf, self) - - def start(self): - """Run each worker in new thread""" - - for i in range(self.conf.worker_threads): - eventlet.spawn_n(Worker().run) - while True: - self.server.listen() - - def new_client(self, unixsocket, address): - """Establish connection with the tcp server""" - - tcpsocket, connected = self.client.connect() - if not connected: - message = "Proxy -> Could not connect with tcp server" - LOG.error(message) - unixsocket.close() - tcpsocket.close() - else: - pc = ProxyConnection(self.conf, unixsocket, tcpsocket) - ConnQ.put(pc) - - -PROXY_OPTS = [ - oslo_config.IntOpt( - 'max_connections', - default=10, - help='Max num of client connections' - ), - oslo_config.IntOpt( - 'worker_threads', - default=10, - help='Number of threads to be used.' - ), - oslo_config.FloatOpt( - 'connect_max_wait_timeout', - default=10, - help='Max time to wait for connect.' - ), - oslo_config.FloatOpt( - 'idle_max_wait_timeout', - default=10, - help='Max time to keep idle channel open.' - ), - oslo_config.FloatOpt( - 'idle_min_wait_timeout', - default=10, - help='Minimum time to wait on idle channel.' - ), - oslo_config.StrOpt( - 'unix_bind_path', - default='/var/run/uds_socket', - help='Path for unix server to bind.' - ), - oslo_config.StrOpt( - 'nfp_controller_ip', - default='172.16.0.3', - help='NFP controllers ip address' - ), - oslo_config.IntOpt( - 'nfp_controller_port', - default='8070', - help='NFP controllers port num' - ) -] - - -def main(argv): - oslo_config.CONF.register_opts(PROXY_OPTS, 'proxy') - oslo_config.CONF(args=sys.argv[1:]) - oslo_logging.setup(oslo_config.CONF, 'nfp') - conf = Configuration(oslo_config.CONF) - Proxy(conf).start() diff --git a/gbpservice/nfp/utils/forked_pdb.py b/gbpservice/nfp/utils/forked_pdb.py deleted file mode 100644 index 99078729d..000000000 --- a/gbpservice/nfp/utils/forked_pdb.py +++ /dev/null @@ -1,38 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import pdb -import sys - -"""For debugging inside a child process. - - import pdb;pdb.set_trace() does not work - with python multiprocessing. - Instead use below pdb class to debug inside - a worker process / child process. -""" - - -class ForkedPdb(pdb.Pdb): - - """A Pdb subclass that may be used - from a forked multiprocessing child - - """ - - def interaction(self, *args, **kwargs): - _stdin = sys.stdin - try: - sys.stdin = open('/dev/stdin', 'r') - pdb.Pdb.interaction(self, *args, **kwargs) - finally: - sys.stdin = _stdin diff --git a/gbpservice/tests/contrib/README b/gbpservice/tests/contrib/README deleted file mode 100644 index 6abc0c67d..000000000 --- a/gbpservice/tests/contrib/README +++ /dev/null @@ -1,3 +0,0 @@ -The files in this directory are intended for use by the -infra jobs that run the various functional test -suite in the gate for the group-based-policy repo. diff --git a/gbpservice/tests/contrib/__init__.py b/gbpservice/tests/contrib/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gbpservice/tests/contrib/devstack/exercises-aim/gbp_aim.sh b/gbpservice/tests/contrib/devstack/exercises-aim/gbp_aim.sh deleted file mode 100755 index 6d00c5df1..000000000 --- a/gbpservice/tests/contrib/devstack/exercises-aim/gbp_aim.sh +++ /dev/null @@ -1,153 +0,0 @@ -#!/usr/bin/env bash - -# **gbp_aim.sh** - -# Sanity check that gbp started if enabled - -echo "*********************************************************************" -echo "Begin DevStack Exercise: $0" -echo "*********************************************************************" - -# Settings -# ======== - -# This script exits on an error so that errors don't compound and you see -# only the first error that occurred. -set -o errexit - -# Keep track of the current directory -EXERCISE_DIR=$(cd $(dirname "$0") && pwd) -TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) - -# Import common functions -source $TOP_DIR/devstack/functions - -# Import configuration -source $TOP_DIR/devstack/openrc - -# Import exercise configuration -#source $TOP_DIR/exerciserc - -source $TOP_DIR/devstack/openrc demo demo - -# Print the commands being run so that we can see the command that triggers -# an error. It is also useful for following allowing as the install occurs. -set -o xtrace - -function confirm_server_active { - local VM_UUID=$1 - if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then - echo "server '$VM_UUID' did not become active!" - false - fi -} - -# Create allow action that can used in several rules -gbp policy-action-create allow --action-type allow - -# Create ICMP rule -gbp policy-classifier-create icmp-traffic --protocol icmp --direction bi -gbp policy-rule-create ping-policy-rule --classifier icmp-traffic --actions allow - -# Create SSH Rule (Optional) -# gbp policy-classifier-create ssh-traffic --protocol tcp --port-range 22 --direction bi -# gbp policy-rule-create ssh-policy-rule --classifier ssh-traffic --actions allow - -# Create HTTP Rule -gbp policy-classifier-create web-traffic --protocol tcp --port-range 80 --direction in -gbp policy-rule-create web-policy-rule --classifier web-traffic --actions allow - -# Create HTTPs Rule -gbp policy-classifier-create secure-web-traffic --protocol tcp --port-range 443 --direction in -gbp policy-rule-create secure-web-policy-rule --classifier secure-web-traffic --actions allow - -# ICMP policy-rule-set -gbp policy-rule-set-create icmp-policy-rule-set --policy-rules ping-policy-rule - -# WEB policy-rule-set -gbp policy-rule-set-create web-policy-rule-set --policy-rules web-policy-rule - -# ====== PROJECT OPERATION ====== -# APG creation -gbp apg-create myapp - -# PTGs creation -gbp group-create --application-policy-group myapp web -gbp group-create --application-policy-group myapp client-1 -gbp group-create --application-policy-group myapp client-2 - -# PT creation -WEB_PORT=$(gbp policy-target-create web-pt-1 --policy-target-group web | awk "/port_id/ {print \$4}") -CLIENT1_PORT=$(gbp policy-target-create client-pt-1 --policy-target-group client-1 | awk "/port_id/ {print \$4}") -CLIENT2_PORT=$(gbp policy-target-create client-pt-2 --policy-target-group client-2 | awk "/port_id/ {print \$4}") - -# Temporarily disable VM launching -# WEB_VM_1_UUID=`nova boot --flavor m1.tiny --image $DEFAULT_IMAGE_NAME --nic port-id=$WEB_PORT web-vm-1 | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'` -# die_if_not_set $LINENO WEB_VM_1_UUID "Failure launching web-vm-1" -# confirm_server_active $WEB_VM_1_UUID - -# CLIENT_VM_1_UUID=`nova boot --flavor m1.tiny --image $DEFAULT_IMAGE_NAME --nic port-id=$CLIENT1_PORT client-vm-1 | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'` -# die_if_not_set $LINENO CLIENT_VM_1_UUID "Failure launching client-vm-1" -# confirm_server_active $CLIENT_VM_1_UUID - -# CLIENT_VM_2_UUID=`nova boot --flavor m1.tiny --image $DEFAULT_IMAGE_NAME --nic port-id=$CLIENT2_PORT client-vm-2 | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'` -# die_if_not_set $LINENO CLIENT_VM_2_UUID "Failure launching client-vm-2" -# confirm_server_active $CLIENT_VM_2_UUID - -####CHECKPOINT: No traffic flows - -# policy-rule-set Association -gbp group-update client-1 --consumed-policy-rule-sets "icmp-policy-rule-set,web-policy-rule-set" -gbp group-update client-2 --consumed-policy-rule-sets "icmp-policy-rule-set,web-policy-rule-set" -gbp group-update web --provided-policy-rule-sets "icmp-policy-rule-set,web-policy-rule-set" - -####CHECKPOINT: ICMP and HTTP work from app to web and vice versa - -gbp policy-rule-set-update web-policy-rule-set --policy-rules "secure-web-policy-rule" - -####CHECKPOINT: HTTP stops working for both the client PTGs, HTTPs is now enabled - -# Since VMs are not being launched, dont delete and wait for them -# nova delete web-vm-1 -# nova delete client-vm-1 -# nova delete client-vm-2 - -#if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q ACTIVE; do sleep 1; done"; then -# die $LINENO "Some VMs failed to shutdown" -#fi - -gbp policy-target-delete web-pt-1 -gbp policy-target-delete client-pt-1 -gbp policy-target-delete client-pt-2 - -# The following tests fixed IP assignment for PTs -WEB_SUBNET=$(gbp ptg-show web | grep subnets | awk '{print $4}') -WEB_GW_IP=$(neutron subnet-show $WEB_SUBNET | grep gateway_ip | awk '{print $4}') -FIXED_IP="${WEB_GW_IP}0" -gbp pt-create --policy-target-group web --fixed-ip subnet_id=$WEB_SUBNET,ip_address=$FIXED_IP pt-fixed-ip -neutron port-show pt_pt-fixed-ip -gbp pt-delete pt-fixed-ip - -gbp group-delete web -gbp group-delete client-1 -gbp group-delete client-2 - -gbp apg-delete myapp - -gbp policy-rule-set-delete icmp-policy-rule-set -gbp policy-rule-set-delete web-policy-rule-set - -gbp policy-rule-delete secure-web-policy-rule -gbp policy-rule-delete web-policy-rule -gbp policy-rule-delete ping-policy-rule - -gbp policy-classifier-delete secure-web-traffic -gbp policy-classifier-delete web-traffic -gbp policy-classifier-delete icmp-traffic - -gbp policy-action-delete allow - -set +o xtrace -echo "*********************************************************************" -echo "SUCCESS: End DevStack Exercise: $0" -echo "*********************************************************************" diff --git a/gbpservice/tests/contrib/devstack/exercises-aim/neutron_aim.sh b/gbpservice/tests/contrib/devstack/exercises-aim/neutron_aim.sh deleted file mode 100755 index f34d89ff6..000000000 --- a/gbpservice/tests/contrib/devstack/exercises-aim/neutron_aim.sh +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env bash - -# **neutron_aim.sh** - -echo "*********************************************************************" -echo "Begin DevStack Exercise: $0" -echo "*********************************************************************" - -# Settings -# ======== - -# This script exits on an error so that errors don't compound and you see -# only the first error that occurred. -set -o errexit - -# Keep track of the current directory -EXERCISE_DIR=$(cd $(dirname "$0") && pwd) -TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) - -# Import common functions -source $TOP_DIR/devstack/functions - -# Import configuration -source $TOP_DIR/devstack/openrc - -# Import exercise configuration -#source $TOP_DIR/exerciserc - -source $TOP_DIR/devstack/openrc demo demo - -VALIDATE_OPTS=${VALIDATE_OPTS:-"--config-file /etc/neutron/neutron.conf --config-file /etc/neutron/plugins/ml2/ml2_conf.ini"} - -# Print the commands being run so that we can see the command that triggers -# an error. It is also useful for following allowing as the install occurs. -set -o xtrace - -# Validate any pre-existing resources -gbp-validate $VALIDATE_OPTS - -# Create network -openstack network create net1 - -# Validate -gbp-validate $VALIDATE_OPTS - -# Delete network -openstack network delete net1 - -# Validate -gbp-validate $VALIDATE_OPTS - -set +o xtrace -echo "*********************************************************************" -echo "SUCCESS: End DevStack Exercise: $0" -echo "*********************************************************************" diff --git a/gbpservice/tests/contrib/devstack/exercises-nfp/fw.sh b/gbpservice/tests/contrib/devstack/exercises-nfp/fw.sh deleted file mode 100755 index 4d267c3b7..000000000 --- a/gbpservice/tests/contrib/devstack/exercises-nfp/fw.sh +++ /dev/null @@ -1,147 +0,0 @@ -#!/usr/bin/env bash - -# **fw.sh** - -# Sanity check that firewall service is created with NFP - -echo "*********************************************************************" -echo "Begin NFP Exercise: $0" -echo "*********************************************************************" - -# Settings -# ======== - -# This script exits on an error so that errors don't compound and you see -# only the first error that occurred. -set -o errexit - -# Keep track of the current directory -EXERCISE_DIR=$(cd $(dirname "$0") && pwd) -TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) - -source $TOP_DIR/openrc neutron service -source $TOP_DIR/exercises/nfp_lib.sh - -create_gbp_resources() { - gbp servicechain-node-create --service-profile base_mode_fw --template-file $TOP_DIR/nfp-templates/fw_template.yml FWNODE - gbp servicechain-spec-create --nodes "FWNODE" fw-chainspec - gbp policy-action-create --action-type REDIRECT --action-value fw-chainspec redirect-to-fw - gbp policy-action-create --action-type ALLOW allow-to-fw - gbp policy-classifier-create --protocol tcp --direction bi fw-web-classifier-tcp - gbp policy-classifier-create --protocol udp --direction bi fw-web-classifier-udp - gbp policy-classifier-create --protocol icmp --direction bi fw-web-classifier-icmp - gbp policy-rule-create --classifier fw-web-classifier-tcp --actions redirect-to-fw fw-web-redirect-rule - gbp policy-rule-create --classifier fw-web-classifier-tcp --actions allow-to-fw fw-web-allow-rule-tcp - gbp policy-rule-create --classifier fw-web-classifier-udp --actions allow-to-fw fw-web-allow-rule-udp - gbp policy-rule-create --classifier fw-web-classifier-icmp --actions allow-to-fw fw-web-allow-rule-icmp - gbp policy-rule-set-create --policy-rules "fw-web-redirect-rule fw-web-allow-rule-tcp fw-web-allow-rule-udp fw-web-allow-rule-icmp" fw-webredirect-ruleset - gbp group-create fw-consumer --consumed-policy-rule-sets "fw-webredirect-ruleset=None" - gbp group-create fw-provider --provided-policy-rule-sets "fw-webredirect-ruleset=None" - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 600 secs. - check_group_status fw-provider 600 -} - -delete_gbp_resources() { - gbp group-delete fw-provider - gbp group-delete fw-consumer - gbp policy-rule-set-delete fw-webredirect-ruleset - gbp policy-rule-delete fw-web-redirect-rule - gbp policy-rule-delete fw-web-allow-rule-tcp - gbp policy-rule-delete fw-web-allow-rule-icmp - gbp policy-rule-delete fw-web-allow-rule-udp - gbp policy-classifier-delete fw-web-classifier-tcp - gbp policy-classifier-delete fw-web-classifier-icmp - gbp policy-classifier-delete fw-web-classifier-udp - gbp policy-action-delete redirect-to-fw - gbp policy-action-delete allow-to-fw - gbp servicechain-spec-delete fw-chainspec - gbp servicechain-node-delete FWNODE - # Added sleep 60 sec to complete delete operation - sleep 60 -} - -validate_gbp_resources() { - ServiceChainInstanceCount=`gbp sci-list -f value | grep fw-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "1" ]; then - echo "Chain creation Succeeded" - else - echo "Chain creation failed" - fi -} - -validate_firewall_resources() { - FirewallRuleCount=`neutron firewall-rule-list -f value | grep Rule | wc -l` - if [ "$FirewallRuleCount" -eq "4" ]; then - echo "Firewall Rule resource created" - else - echo "Firewall Rule resource not created" - fi - - FirewallPolicyCount=`neutron firewall-policy-list -f value | grep fw | wc -l` - if [ "$FirewallPolicyCount" -eq "1" ]; then - echo "Firewall Policy resource created" - else - echo "Firewall Policy resource not created" - fi - - FirewallCount=`neutron firewall-list -f value | wc -l` - if [ "$FirewallCount" -eq "1" ]; then - echo "Firewall resource created" - FirewallUUID=`neutron firewall-list -f value | awk '{print $1}'` - FirewallStatus=`neutron firewall-show $FirewallUUID -f value -c status` - echo "Firewall resource is in $FirewallStatus state" - else - echo "Firewall resource not created" - fi -} - -update_gbp_resources() { - # Update existing chain, by removing 2 rules - #gbp servicechain-node-update FWNODE --template-file $TOP_DIR/nfp-templates/fw_updated_template.yml - - #FirewallRuleCount=`neutron firewall-rule-list -f value | wc -l` - #if [ "$FirewallRuleCount" -eq "2" ]; then - # echo "Chain created" - #else - # echo "Chain not created" - #fi - - gbp group-delete fw-provider - # Added sleep 60 sec to complete delete operation - sleep 60 - gbp group-delete fw-consumer - ServiceChainInstanceCount=`gbp sci-list -f value | grep fw-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "0" ]; then - echo "Chain deleted" - else - echo "Chain not deleted" - fi - - # Service chain creation/deletion through PRS update - gbp group-create fw-consumer --consumed-policy-rule-sets "fw-webredirect-ruleset=None" - gbp group-create fw-provider - ServiceChainInstanceCount=`gbp sci-list -f value | grep fw-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "0" ]; then - echo "Chain not created" - else - echo "Chain not deleted" - fi - - gbp group-update fw-provider --provided-policy-rule-sets "fw-webredirect-ruleset=None" - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 600 secs. - check_group_status fw-provider 600 - ServiceChainInstanceCount=`gbp sci-list -f value | grep fw-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "1" ]; then - echo "Chain created" - else - echo "Chain not created" - fi -} - -create_gbp_resources -validate_gbp_resources -validate_firewall_resources - -update_gbp_resources - -delete_gbp_resources diff --git a/gbpservice/tests/contrib/devstack/exercises-nfp/fw_lb.sh b/gbpservice/tests/contrib/devstack/exercises-nfp/fw_lb.sh deleted file mode 100755 index 692793670..000000000 --- a/gbpservice/tests/contrib/devstack/exercises-nfp/fw_lb.sh +++ /dev/null @@ -1,207 +0,0 @@ -#!/usr/bin/env bash - -# **fw_lb.sh** - -# Sanity check that firewall and loadbalancer service chain is created with NFP - -echo "*********************************************************************" -echo "Begin NFP Exercise: $0" -echo "*********************************************************************" - -# Settings -# ======== - -# This script exits on an error so that errors don't compound and you see -# only the first error that occurred. -set -o errexit - -# Keep track of the current directory -EXERCISE_DIR=$(cd $(dirname "$0") && pwd) -TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) - -source $TOP_DIR/openrc neutron service -source $TOP_DIR/exercises/nfp_lib.sh - -create_gbp_resources() { - # E-W insertion - gbp servicechain-node-create --service-profile base_mode_fw --template-file $TOP_DIR/nfp-templates/fw_template.yml FW_LB-FWNODE - gbp servicechain-node-create --service-profile base_mode_lb --template-file $TOP_DIR/nfp-templates/haproxy_lbaasv2.template FW_LB-LBNODE - gbp servicechain-spec-create --nodes "FW_LB-FWNODE FW_LB-LBNODE" fw_lb_chainspec - gbp policy-action-create --action-type REDIRECT --action-value fw_lb_chainspec redirect-to-fw_lb - gbp policy-classifier-create --protocol tcp --direction bi fw_lb-webredirect - gbp policy-rule-create --classifier fw_lb-webredirect --actions redirect-to-fw_lb fw_lb-web-redirect-rule - gbp policy-rule-set-create --policy-rules "fw_lb-web-redirect-rule" fw_lb-webredirect-ruleset - gbp network-service-policy-create --network-service-params type=ip_single,name=vip_ip,value=self_subnet fw_lb_nsp - gbp group-create fw_lb-consumer --consumed-policy-rule-sets "fw_lb-webredirect-ruleset=None" - gbp group-create fw_lb-provider --provided-policy-rule-sets "fw_lb-webredirect-ruleset=None" --network-service-policy fw_lb_nsp - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 600 secs. - check_group_status fw_lb-provider 600 -} - -delete_gbp_resources() { - gbp group-delete fw_lb-provider - gbp group-delete fw_lb-consumer - gbp network-service-policy-delete fw_lb_nsp - gbp policy-rule-set-delete fw_lb-webredirect-ruleset - gbp policy-rule-delete fw_lb-web-redirect-rule - gbp policy-classifier-delete fw_lb-webredirect - gbp policy-action-delete redirect-to-fw_lb - gbp servicechain-spec-delete fw_lb_chainspec - gbp servicechain-node-delete FW_LB-LBNODE - gbp servicechain-node-delete FW_LB-FWNODE - # Added sleep 60 sec to complete delete operation - sleep 60 -} - -validate_gbp_resources() { - ServiceChainInstanceCount=`gbp sci-list -f value | grep fw_lb-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "1" ]; then - echo "Chain creation Succeeded" - else - echo "Chain creation failed" - fi - - ServiceChainNodeCount=`gbp scn-list -f value | grep FW_LB | wc -l` - if [ "$ServiceChainNodeCount" -eq "2" ]; then - echo "Network function creation Succeeded" - else - echo "Network function creation failed" - fi -} - -validate_firewall_resources() { - FirewallRuleCount=`neutron firewall-rule-list -f value | grep Rule | wc -l` - if [ "$FirewallRuleCount" -eq "4" ]; then - echo "Firewall Rule resource created" - else - echo "Firewall Rule resource not created" - fi - - FirewallPolicyCount=`neutron firewall-policy-list -f value | grep fw | wc -l` - if [ "$FirewallPolicyCount" -eq "1" ]; then - echo "Firewall Policy resource created" - else - echo "Firewall Policy resource not created" - fi - - FirewallCount=`neutron firewall-list -f value | wc -l` - if [ "$FirewallCount" -eq "1" ]; then - echo "Firewall resource created" - FirewallUUID=`neutron firewall-list -f value | awk '{print $1}'` - FirewallStatus=`neutron firewall-show $FirewallUUID -f value -c status` - echo "Firewall resource is in $FirewallStatus state" - else - echo "Firewall resource not created" - fi -} - -validate_loadbalancer_resources() { - LBPoolCount=`neutron lbaas-pool-list -f value | wc -l` - if [ "$LBPoolCount" -eq "1" ]; then - echo "LB Pool resource created" - LBPoolUUID=`neutron lbaas-pool-list -f value | awk '{print $1}'` - #LBPoolStatus=`neutron lbaas-pool-show $LBPoolUUID -f value -c status` - #echo "LB Pool resource is in $LBPoolStatus state" - else - echo "LB Pool resource not created" - fi - - LBHMCount=`neutron lbaas-healthmonitor-list -f value | wc -l` - if [ "$LBHMCount" -eq "1" ]; then - echo "LB Healthmonitor resource created" - else - echo "LB Healthmonitor resource not created" - fi - - gbp policy-target-create --policy-target-group fw_lb-provider provider_pt1 - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 300 secs. - check_group_status fw_lb-provider 300 - LBMemberCount=`neutron lbaas-member-list $LBPoolUUID -f value | wc -l` - if [ "$LBMemberCount" -eq "1" ]; then - echo "LB Member resource created" - else - echo "LB Member resource not created" - fi - - gbp policy-target-create --policy-target-group fw_lb-provider provider_pt2 - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 300 secs. - check_group_status fw_lb-provider 300 - LBMemberCount=`neutron lbaas-member-list $LBPoolUUID -f value | wc -l` - if [ "$LBMemberCount" -eq "2" ]; then - echo "LB Member resource created" - else - echo "LB Member resource not created" - fi - - gbp policy-target-delete provider_pt1 - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 300 secs. - check_group_status fw_lb-provider 300 - LBMemberCount=`neutron lbaas-member-list $LBPoolUUID -f value | wc -l` - if [ "$LBMemberCount" -eq "1" ]; then - echo "LB Member resource deleted" - else - echo "LB Member resource not deleted" - fi - - gbp policy-target-delete provider_pt2 - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 300 secs. - check_group_status fw_lb-provider 300 - LBMemberCount=`neutron lbaas-member-list $LBPoolUUID -f value | wc -l` - if [ "$LBMemberCount" -eq "0" ]; then - echo "LB Member resource deleted" - else - echo "LB Member resource not deleted" - fi -} - -update_gbp_resources() { - # Update existing chain, by removing 2 rules - #gbp servicechain-node-update FW_LB-FWNODE --template-file $TOP_DIR/nfp-templates/fw_updated_template.yml - - #FirewallRuleCount=`neutron firewall-rule-list -f value | wc -l` - #if [ "$FirewallRuleCount" -eq "2" ]; then - # echo "Chain created" - #else - # echo "Chain not created" - #fi - - gbp group-delete fw_lb-provider - # Added sleep 60 sec to complete delete operation - sleep 60 - gbp group-delete fw_lb-consumer - ServiceChainInstanceCount=`gbp sci-list -f value | grep fw_lb-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "0" ]; then - echo "Chain deleted" - else - echo "Chain not deleted" - fi - - # Service chain creation/deletion through PRS update - gbp group-create fw_lb-consumer --consumed-policy-rule-sets "fw_lb-webredirect-ruleset=None" - gbp group-create fw_lb-provider - ServiceChainInstanceCount=`gbp sci-list -f value | grep fw_lb-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "0" ]; then - echo "Chain not created" - else - echo "Chain not deleted" - fi - - gbp group-update fw_lb-provider --provided-policy-rule-sets "fw_lb-webredirect-ruleset=None" --network-service-policy fw_lb_nsp - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 600 secs. - check_group_status fw_lb-provider 600 - ServiceChainInstanceCount=`gbp sci-list -f value | grep fw_lb-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "1" ]; then - echo "Chain created" - else - echo "Chain not created" - fi -} - -create_gbp_resources -validate_gbp_resources -validate_firewall_resources -validate_loadbalancer_resources - -update_gbp_resources - -delete_gbp_resources diff --git a/gbpservice/tests/contrib/devstack/exercises-nfp/fw_vm.sh b/gbpservice/tests/contrib/devstack/exercises-nfp/fw_vm.sh deleted file mode 100755 index ca4c6bbc6..000000000 --- a/gbpservice/tests/contrib/devstack/exercises-nfp/fw_vm.sh +++ /dev/null @@ -1,147 +0,0 @@ -#!/usr/bin/env bash - -# **fw_vm.sh** - -# Sanity check that firewall(in service VM) service is created with NFP - -echo "*********************************************************************" -echo "Begin NFP Exercise: $0" -echo "*********************************************************************" - -# Settings -# ======== - -# This script exits on an error so that errors don't compound and you see -# only the first error that occurred. -set -o errexit - -# Keep track of the current directory -EXERCISE_DIR=$(cd $(dirname "$0") && pwd) -TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) - -source $TOP_DIR/openrc neutron service -source $TOP_DIR/exercises/nfp_lib.sh - -create_gbp_resources() { - gbp servicechain-node-create --service-profile base_mode_fw_vm --config 'custom_json:{"mimetype": "config/custom+json","rules": [{"action": "log", "name": "tcp", "service": "tcp/80"}, {"action": "log", "name": "tcp", "service": "tcp/8080"}, {"action": "accept", "name": "tcp", "service": "tcp/22"}, {"action": "accept", "name": "icmp", "service": "icmp"}]}' FWNODE - gbp servicechain-spec-create --nodes "FWNODE" fw-chainspec - gbp policy-action-create --action-type REDIRECT --action-value fw-chainspec redirect-to-fw - gbp policy-action-create --action-type ALLOW allow-to-fw - gbp policy-classifier-create --protocol tcp --direction bi fw-web-classifier-tcp - gbp policy-classifier-create --protocol udp --direction bi fw-web-classifier-udp - gbp policy-classifier-create --protocol icmp --direction bi fw-web-classifier-icmp - gbp policy-rule-create --classifier fw-web-classifier-tcp --actions redirect-to-fw fw-web-redirect-rule - gbp policy-rule-create --classifier fw-web-classifier-tcp --actions allow-to-fw fw-web-allow-rule-tcp - gbp policy-rule-create --classifier fw-web-classifier-udp --actions allow-to-fw fw-web-allow-rule-udp - gbp policy-rule-create --classifier fw-web-classifier-icmp --actions allow-to-fw fw-web-allow-rule-icmp - gbp policy-rule-set-create --policy-rules "fw-web-redirect-rule fw-web-allow-rule-tcp fw-web-allow-rule-udp fw-web-allow-rule-icmp" fw-webredirect-ruleset - gbp group-create fw-consumer --consumed-policy-rule-sets "fw-webredirect-ruleset=None" - gbp group-create fw-provider --provided-policy-rule-sets "fw-webredirect-ruleset=None" - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 600 secs. - check_group_status fw-provider 600 -} - -delete_gbp_resources() { - gbp group-delete fw-provider - gbp group-delete fw-consumer - gbp policy-rule-set-delete fw-webredirect-ruleset - gbp policy-rule-delete fw-web-redirect-rule - gbp policy-rule-delete fw-web-allow-rule-tcp - gbp policy-rule-delete fw-web-allow-rule-icmp - gbp policy-rule-delete fw-web-allow-rule-udp - gbp policy-classifier-delete fw-web-classifier-tcp - gbp policy-classifier-delete fw-web-classifier-icmp - gbp policy-classifier-delete fw-web-classifier-udp - gbp policy-action-delete redirect-to-fw - gbp policy-action-delete allow-to-fw - gbp servicechain-spec-delete fw-chainspec - gbp servicechain-node-delete FWNODE - # Added sleep of 300 secs to complete delete operation - sleep 300 -} - -validate_gbp_resources() { - ServiceChainInstanceCount=`gbp sci-list -f value | grep fw-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "1" ]; then - echo "Chain creation Succeeded" - else - echo "Chain creation failed" - fi -} - -validate_firewall_resources() { - FirewallRuleCount=`neutron firewall-rule-list -f value | grep Rule | wc -l` - if [ "$FirewallRuleCount" -eq "4" ]; then - echo "Firewall Rule resource created" - else - echo "Firewall Rule resource not created" - fi - - FirewallPolicyCount=`neutron firewall-policy-list -f value | grep fw | wc -l` - if [ "$FirewallPolicyCount" -eq "1" ]; then - echo "Firewall Policy resource created" - else - echo "Firewall Policy resource not created" - fi - - FirewallCount=`neutron firewall-list -f value | wc -l` - if [ "$FirewallCount" -eq "1" ]; then - echo "Firewall resource created" - FirewallUUID=`neutron firewall-list -f value | awk '{print $1}'` - FirewallStatus=`neutron firewall-show $FirewallUUID -f value -c status` - echo "Firewall resource is in $FirewallStatus state" - else - echo "Firewall resource not created" - fi -} - -update_gbp_resources() { - # Update existing chain, by removing 2 rules - #gbp servicechain-node-update FWNODE --template-file $TOP_DIR/nfp-templates/fw_updated_template.yml - - #FirewallRuleCount=`neutron firewall-rule-list -f value | wc -l` - #if [ "$FirewallRuleCount" -eq "2" ]; then - # echo "Chain created" - #else - # echo "Chain not created" - #fi - - gbp group-delete fw-provider - # Added sleep of 300 secs to complete delete operation - sleep 300 - gbp group-delete fw-consumer - ServiceChainInstanceCount=`gbp sci-list -f value | grep fw-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "0" ]; then - echo "Chain deleted" - else - echo "Chain not deleted" - fi - - # Service chain creation/deletion through PRS update - gbp group-create fw-consumer --consumed-policy-rule-sets "fw-webredirect-ruleset=None" - gbp group-create fw-provider - ServiceChainInstanceCount=`gbp sci-list -f value | grep fw-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "0" ]; then - echo "Chain not created" - else - echo "Chain not deleted" - fi - - gbp group-update fw-provider --provided-policy-rule-sets "fw-webredirect-ruleset=None" - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 600 secs. - check_group_status fw-provider 600 - ServiceChainInstanceCount=`gbp sci-list -f value | grep fw-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "1" ]; then - echo "Chain created" - else - echo "Chain not created" - fi -} - -create_gbp_resources -validate_gbp_resources -validate_firewall_resources - -update_gbp_resources - -delete_gbp_resources diff --git a/gbpservice/tests/contrib/devstack/exercises-nfp/fw_vm_lb.sh b/gbpservice/tests/contrib/devstack/exercises-nfp/fw_vm_lb.sh deleted file mode 100755 index fbd15cd51..000000000 --- a/gbpservice/tests/contrib/devstack/exercises-nfp/fw_vm_lb.sh +++ /dev/null @@ -1,207 +0,0 @@ -#!/usr/bin/env bash - -# **fw_vm_lb.sh** - -# Sanity check that firewall(in service VM) and loadbalancer service chain is created with NFP - -echo "*********************************************************************" -echo "Begin NFP Exercise: $0" -echo "*********************************************************************" - -# Settings -# ======== - -# This script exits on an error so that errors don't compound and you see -# only the first error that occurred. -set -o errexit - -# Keep track of the current directory -EXERCISE_DIR=$(cd $(dirname "$0") && pwd) -TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) - -source $TOP_DIR/openrc neutron service -source $TOP_DIR/exercises/nfp_lib.sh - -create_gbp_resources() { - # E-W insertion - gbp servicechain-node-create --service-profile base_mode_fw_vm --config 'custom_json:{"mimetype": "config/custom+json","rules": [{"action": "log", "name": "tcp", "service": "tcp/80"}, {"action": "log", "name": "tcp", "service": "tcp/8080"}, {"action": "accept", "name": "tcp", "service": "tcp/22"}, {"action": "accept", "name": "icmp", "service": "icmp"}]}' FW_LB-FWNODE - gbp servicechain-node-create --service-profile base_mode_lb --template-file $TOP_DIR/nfp-templates/haproxy_lbaasv2.template FW_LB-LBNODE - gbp servicechain-spec-create --nodes "FW_LB-FWNODE FW_LB-LBNODE" fw_lb_chainspec - gbp policy-action-create --action-type REDIRECT --action-value fw_lb_chainspec redirect-to-fw_lb - gbp policy-classifier-create --protocol tcp --direction bi fw_lb-webredirect - gbp policy-rule-create --classifier fw_lb-webredirect --actions redirect-to-fw_lb fw_lb-web-redirect-rule - gbp policy-rule-set-create --policy-rules "fw_lb-web-redirect-rule" fw_lb-webredirect-ruleset - gbp network-service-policy-create --network-service-params type=ip_single,name=vip_ip,value=self_subnet fw_lb_nsp - gbp group-create fw_lb-consumer --consumed-policy-rule-sets "fw_lb-webredirect-ruleset=None" - gbp group-create fw_lb-provider --provided-policy-rule-sets "fw_lb-webredirect-ruleset=None" --network-service-policy fw_lb_nsp - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 600 secs. - check_group_status fw_lb-provider 600 -} - -delete_gbp_resources() { - gbp group-delete fw_lb-provider - gbp group-delete fw_lb-consumer - gbp network-service-policy-delete fw_lb_nsp - gbp policy-rule-set-delete fw_lb-webredirect-ruleset - gbp policy-rule-delete fw_lb-web-redirect-rule - gbp policy-classifier-delete fw_lb-webredirect - gbp policy-action-delete redirect-to-fw_lb - gbp servicechain-spec-delete fw_lb_chainspec - gbp servicechain-node-delete FW_LB-LBNODE - gbp servicechain-node-delete FW_LB-FWNODE - # Added sleep of 300 secs to complete delete operation - sleep 300 -} - -validate_gbp_resources() { - ServiceChainInstanceCount=`gbp sci-list -f value | grep fw_lb-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "1" ]; then - echo "Chain creation Succeeded" - else - echo "Chain creation failed" - fi - - ServiceChainNodeCount=`gbp scn-list -f value | grep FW_LB | wc -l` - if [ "$ServiceChainNodeCount" -eq "2" ]; then - echo "Network function creation Succeeded" - else - echo "Network function creation failed" - fi -} - -validate_firewall_resources() { - FirewallRuleCount=`neutron firewall-rule-list -f value | grep Rule | wc -l` - if [ "$FirewallRuleCount" -eq "4" ]; then - echo "Firewall Rule resource created" - else - echo "Firewall Rule resource not created" - fi - - FirewallPolicyCount=`neutron firewall-policy-list -f value | grep fw | wc -l` - if [ "$FirewallPolicyCount" -eq "1" ]; then - echo "Firewall Policy resource created" - else - echo "Firewall Policy resource not created" - fi - - FirewallCount=`neutron firewall-list -f value | wc -l` - if [ "$FirewallCount" -eq "1" ]; then - echo "Firewall resource created" - FirewallUUID=`neutron firewall-list -f value | awk '{print $1}'` - FirewallStatus=`neutron firewall-show $FirewallUUID -f value -c status` - echo "Firewall resource is in $FirewallStatus state" - else - echo "Firewall resource not created" - fi -} - -validate_loadbalancer_resources() { - LBPoolCount=`neutron lbaas-pool-list -f value | wc -l` - if [ "$LBPoolCount" -eq "1" ]; then - echo "LB Pool resource created" - LBPoolUUID=`neutron lbaas-pool-list -f value | awk '{print $1}'` - #LBPoolStatus=`neutron lb-pool-show $LBPoolUUID -f value -c status` - #echo "LB Pool resource is in $LBPoolStatus state" - else - echo "LB Pool resource not created" - fi - - LBHMCount=`neutron lbaas-healthmonitor-list -f value | wc -l` - if [ "$LBHMCount" -eq "1" ]; then - echo "LB Healthmonitor resource created" - else - echo "LB Healthmonitor resource not created" - fi - - gbp policy-target-create --policy-target-group fw_lb-provider provider_pt1 - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 300 secs. - check_group_status fw_lb-provider 300 - LBMemberCount=`neutron lbaas-member-list $LBPoolUUID -f value | wc -l` - if [ "$LBMemberCount" -eq "1" ]; then - echo "LB Member resource created" - else - echo "LB Member resource not created" - fi - - gbp policy-target-create --policy-target-group fw_lb-provider provider_pt2 - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 300 secs. - check_group_status fw_lb-provider 300 - LBMemberCount=`neutron lbaas-member-list $LBPoolUUID -f value | wc -l` - if [ "$LBMemberCount" -eq "2" ]; then - echo "LB Member resource created" - else - echo "LB Member resource not created" - fi - - gbp policy-target-delete provider_pt1 - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 300 secs. - check_group_status fw_lb-provider 300 - LBMemberCount=`neutron lbaas-member-list $LBPoolUUID -f value | wc -l` - if [ "$LBMemberCount" -eq "1" ]; then - echo "LB Member resource deleted" - else - echo "LB Member resource not deleted" - fi - - gbp policy-target-delete provider_pt2 - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 300 secs. - check_group_status fw_lb-provider 300 - LBMemberCount=`neutron lbaas-member-list $LBPoolUUID -f value | wc -l` - if [ "$LBMemberCount" -eq "0" ]; then - echo "LB Member resource deleted" - else - echo "LB Member resource not deleted" - fi -} - -update_gbp_resources() { - # Update existing chain, by removing 2 rules - #gbp servicechain-node-update FW_LB-FWNODE --template-file $TOP_DIR/nfp-templates/fw_updated_template.yml - - #FirewallRuleCount=`neutron firewall-rule-list -f value | wc -l` - #if [ "$FirewallRuleCount" -eq "2" ]; then - # echo "Chain created" - #else - # echo "Chain not created" - #fi - - gbp group-delete fw_lb-provider - # Added sleep of 300 secs to complete delete operation - sleep 300 - gbp group-delete fw_lb-consumer - ServiceChainInstanceCount=`gbp sci-list -f value | grep fw_lb-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "0" ]; then - echo "Chain deleted" - else - echo "Chain not deleted" - fi - - # Service chain creation/deletion through PRS update - gbp group-create fw_lb-consumer --consumed-policy-rule-sets "fw_lb-webredirect-ruleset=None" - gbp group-create fw_lb-provider - ServiceChainInstanceCount=`gbp sci-list -f value | grep fw_lb-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "0" ]; then - echo "Chain not created" - else - echo "Chain not deleted" - fi - - gbp group-update fw_lb-provider --provided-policy-rule-sets "fw_lb-webredirect-ruleset=None" --network-service-policy fw_lb_nsp - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 600 secs. - check_group_status fw_lb-provider 600 - ServiceChainInstanceCount=`gbp sci-list -f value | grep fw_lb-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "1" ]; then - echo "Chain created" - else - echo "Chain not created" - fi -} - -create_gbp_resources -validate_gbp_resources -validate_firewall_resources -validate_loadbalancer_resources - -update_gbp_resources - -delete_gbp_resources diff --git a/gbpservice/tests/contrib/devstack/exercises-nfp/lb.sh b/gbpservice/tests/contrib/devstack/exercises-nfp/lb.sh deleted file mode 100755 index fd8be09c7..000000000 --- a/gbpservice/tests/contrib/devstack/exercises-nfp/lb.sh +++ /dev/null @@ -1,167 +0,0 @@ - -#!/usr/bin/env bash - -# **lb.sh** - -# Sanity check that loadbalancer service is created with NFP - -echo "*********************************************************************" -echo "Begin NFP Exercise: $0" -echo "*********************************************************************" - -# Settings -# ======== - -# This script exits on an error so that errors don't compound and you see -# only the first error that occurred. -set -o errexit - -# Keep track of the current directory -EXERCISE_DIR=$(cd $(dirname "$0") && pwd) -TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) - -source $TOP_DIR/openrc neutron service -source $TOP_DIR/exercises/nfp_lib.sh - -create_gbp_resources() { - # E-W insertion - gbp servicechain-node-create --service-profile base_mode_lb --template-file $TOP_DIR/nfp-templates/haproxy_lbaasv2.template LB-NODE - gbp servicechain-spec-create --nodes "LB-NODE" lb_chainspec - gbp policy-action-create --action-type REDIRECT --action-value lb_chainspec redirect-to-lb - gbp policy-classifier-create --protocol tcp --direction bi lb-webredirect - gbp policy-rule-create --classifier lb-webredirect --actions redirect-to-lb lb-webredirect-rule - gbp policy-rule-set-create --policy-rules "lb-webredirect-rule" lb-webredirect-ruleset - gbp network-service-policy-create --network-service-params type=ip_single,name=vip_ip,value=self_subnet lb_nsp - gbp group-create lb-consumer --consumed-policy-rule-sets "lb-webredirect-ruleset=None" - gbp group-create lb-provider --provided-policy-rule-sets "lb-webredirect-ruleset=None" --network-service-policy lb_nsp - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 600 secs. - check_group_status lb-provider 600 -} - -delete_gbp_resources() { - gbp group-delete lb-consumer - gbp group-delete lb-provider - gbp network-service-policy-delete lb_nsp - gbp policy-rule-set-delete lb-webredirect-ruleset - gbp policy-rule-delete lb-webredirect-rule - gbp policy-classifier-delete lb-webredirect - gbp policy-action-delete redirect-to-lb - gbp servicechain-spec-delete lb_chainspec - gbp servicechain-node-delete LB-NODE - # Added sleep 60 sec to complete delete operation - sleep 60 -} - -validate_gbp_resources() { - ServiceChainInstanceCount=`gbp sci-list -f value | grep lb-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "1" ]; then - echo "Chain creation Succeeded" - else - echo "Chain creation failed" - fi -} - -validate_loadbalancer_resources() { - LBPoolCount=`neutron lbaas-pool-list -f value | wc -l` - if [ "$LBPoolCount" -eq "1" ]; then - echo "LB Pool resources created" - LBPoolUUID=`neutron lbaas-pool-list -f value | awk '{print $1}'` - #PoolRsourcesList=$(neutron lbaas-pool-list -f value -c id) - #for pool in $(echo $PoolRsourcesList | tr " " "\n") - #do - # LBPoolStatus=`neutron lbaas-pool-show $LBPoolUUID -f value -c status` - # echo "LB Pool resource is in $LBPoolStatus state" - #done - else - echo "LB Pool resource not created" - fi - - LBHMCount=`neutron lbaas-healthmonitor-list -f value | wc -l` - if [ "$LBHMCount" -eq "1" ]; then - echo "LB Healthmonitor resource created" - else - echo "LB Healthmonitor resource not created" - fi - - gbp policy-target-create --policy-target-group lb-provider provider_pt1 - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 300 secs. - check_group_status lb-provider 300 - LBMemberCount=`neutron lbaas-member-list $LBPoolUUID -f value | wc -l` - if [ "$LBMemberCount" -eq "1" ]; then - echo "LB Member resource created" - else - echo "LB Member resource not created" - fi - - gbp policy-target-create --policy-target-group lb-provider provider_pt2 - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 300 secs. - check_group_status lb-provider 300 - LBMemberCount=`neutron lbaas-member-list $LBPoolUUID -f value | wc -l` - if [ "$LBMemberCount" -eq "2" ]; then - echo "LB Member resource created" - else - echo "LB Member resource not created" - fi - - gbp policy-target-delete provider_pt1 - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 300 secs. - check_group_status lb-provider 300 - LBMemberCount=`neutron lbaas-member-list $LBPoolUUID -f value | wc -l` - if [ "$LBMemberCount" -eq "1" ]; then - echo "LB Member resource deleted" - else - echo "LB Member resource not deleted" - fi - - gbp policy-target-delete provider_pt2 - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 300 secs. - check_group_status lb-provider 300 - LBMemberCount=`neutron lbaas-member-list $LBPoolUUID -f value | wc -l` - if [ "$LBMemberCount" -eq "0" ]; then - echo "LB Member resource deleted" - else - echo "LB Member resource not deleted" - fi - -} - -update_gbp_resources() { - gbp group-delete lb-provider - # Added sleep 60 sec to complete delete operation - sleep 60 - gbp group-delete lb-consumer - ServiceChainInstanceCount=`gbp sci-list -f value | grep lb-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "0" ]; then - echo "Chain deleted" - else - echo "Chain not deleted" - fi - - # Service chain creation/deletion through PRS update - gbp group-create lb-consumer --consumed-policy-rule-sets "lb-webredirect-ruleset=None" - gbp group-create lb-provider - ServiceChainInstanceCount=`gbp sci-list -f value | grep lb-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "0" ]; then - echo "Chain not created" - else - echo "Chain not deleted" - fi - - gbp group-update lb-provider --provided-policy-rule-sets "lb-webredirect-ruleset=None" --network-service-policy lb_nsp - # Poll for group status till it becomes ACTIVE/ERROR. Polling timeout is 600 secs. - check_group_status lb-provider 600 - ServiceChainInstanceCount=`gbp sci-list -f value | grep lb-provider | wc -l` - if [ "$ServiceChainInstanceCount" -eq "1" ]; then - echo "Chain created" - else - echo "Chain not created" - fi -} - -create_gbp_resources -validate_gbp_resources -validate_loadbalancer_resources - -update_gbp_resources - -delete_gbp_resources diff --git a/gbpservice/tests/contrib/devstack/exercises-nfp/nfp_lib.sh b/gbpservice/tests/contrib/devstack/exercises-nfp/nfp_lib.sh deleted file mode 100755 index 984384e7d..000000000 --- a/gbpservice/tests/contrib/devstack/exercises-nfp/nfp_lib.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/bash - -check_group_status(){ - ptg_name=$1 - timeout=$2 - curr_time=0 - while [ $curr_time -lt $timeout ]; - do - ptg_status=$(gbp group-show $ptg_name | grep -w 'status' | awk '{print $4}') - if [ 'ACTIVE' == $ptg_status ];then - echo "group $ptg_name becomes ACTIVE after $curr_time secs" - break - elif [ 'ERROR' == $ptg_status ];then - echo "group $ptg_name went to ERROR state after $curr_time secs" - break - fi - sleep 5 - curr_time=$((curr_time + 5)) - done -} diff --git a/gbpservice/tests/contrib/devstack/exercises/flavors.sh b/gbpservice/tests/contrib/devstack/exercises/flavors.sh deleted file mode 100755 index 6e403f639..000000000 --- a/gbpservice/tests/contrib/devstack/exercises/flavors.sh +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env bash - -# **flavor.sh** - -# Sanity check that flavors plugin works with gbp - -echo "*********************************************************************" -echo "Begin DevStack Exercise: $0" -echo "*********************************************************************" - -# Settings -# ======== - -# This script exits on an error so that errors don't compound and you see -# only the first error that occurred. -set -o errexit - -# Keep track of the current directory -EXERCISE_DIR=$(cd $(dirname "$0") && pwd) -TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) - -# Import common functions -source $TOP_DIR/functions - -# Import configuration -source $TOP_DIR/openrc - -# Import exercise configuration -source $TOP_DIR/exerciserc - -source $TOP_DIR/openrc demo demo - -# Print the commands being run so that we can see the command that triggers -# an error. It is also useful for following allowing as the install occurs. -set -o xtrace - -neutron flavor-list - -set +o xtrace -echo "*********************************************************************" -echo "SUCCESS: End DevStack Exercise: $0" -echo "*********************************************************************" diff --git a/gbpservice/tests/contrib/devstack/exercises/gbp.sh b/gbpservice/tests/contrib/devstack/exercises/gbp.sh deleted file mode 100755 index d26c133a7..000000000 --- a/gbpservice/tests/contrib/devstack/exercises/gbp.sh +++ /dev/null @@ -1,153 +0,0 @@ -#!/usr/bin/env bash - -# **gbp.sh** - -# Sanity check that gbp started if enabled - -echo "*********************************************************************" -echo "Begin DevStack Exercise: $0" -echo "*********************************************************************" - -# Settings -# ======== - -# This script exits on an error so that errors don't compound and you see -# only the first error that occurred. -set -o errexit - -# Keep track of the current directory -EXERCISE_DIR=$(cd $(dirname "$0") && pwd) -TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) - -# Import common functions -source $TOP_DIR/functions - -# Import configuration -source $TOP_DIR/openrc - -# Import exercise configuration -source $TOP_DIR/exerciserc - -source $TOP_DIR/openrc demo demo - -# Print the commands being run so that we can see the command that triggers -# an error. It is also useful for following allowing as the install occurs. -set -o xtrace - -ACTIVE_TIMEOUT=240 - -function confirm_server_active { - local VM_UUID=$1 - if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then - echo "server '$VM_UUID' did not become active!" - false - fi -} - -# Create allow action that can used in several rules -gbp policy-action-create allow --action-type allow - -# Create ICMP rule -gbp policy-classifier-create icmp-traffic --protocol icmp --direction bi -gbp policy-rule-create ping-policy-rule --classifier icmp-traffic --actions allow - -# Create SSH Rule (Optional) -# gbp policy-classifier-create ssh-traffic --protocol tcp --port-range 22 --direction bi -# gbp policy-rule-create ssh-policy-rule --classifier ssh-traffic --actions allow - -# Create HTTP Rule -gbp policy-classifier-create web-traffic --protocol tcp --port-range 80 --direction in -gbp policy-rule-create web-policy-rule --classifier web-traffic --actions allow - -# Create HTTPs Rule -gbp policy-classifier-create secure-web-traffic --protocol tcp --port-range 443 --direction in -gbp policy-rule-create secure-web-policy-rule --classifier secure-web-traffic --actions allow - -# ICMP policy-rule-set -gbp policy-rule-set-create icmp-policy-rule-set --policy-rules ping-policy-rule - -# WEB policy-rule-set -gbp policy-rule-set-create web-policy-rule-set --policy-rules web-policy-rule - -# ====== PROJECT OPERATION ====== -# APG creation -gbp apg-create myapp - -# PTGs creation -gbp group-create --application-policy-group myapp web -gbp group-create --application-policy-group myapp client-1 -gbp group-create --application-policy-group myapp client-2 - -# PT creation -WEB_PORT=$(gbp policy-target-create web-pt-1 --policy-target-group web | awk "/port_id/ {print \$4}") -CLIENT1_PORT=$(gbp policy-target-create client-pt-1 --policy-target-group client-1 | awk "/port_id/ {print \$4}") -CLIENT2_PORT=$(gbp policy-target-create client-pt-2 --policy-target-group client-2 | awk "/port_id/ {print \$4}") - -WEB_VM_1_UUID=`nova boot --flavor m1.tiny --image $DEFAULT_IMAGE_NAME --nic port-id=$WEB_PORT web-vm-1 | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'` -die_if_not_set $LINENO WEB_VM_1_UUID "Failure launching web-vm-1" -confirm_server_active $WEB_VM_1_UUID - -CLIENT_VM_1_UUID=`nova boot --flavor m1.tiny --image $DEFAULT_IMAGE_NAME --nic port-id=$CLIENT1_PORT client-vm-1 | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'` -die_if_not_set $LINENO CLIENT_VM_1_UUID "Failure launching client-vm-1" -confirm_server_active $CLIENT_VM_1_UUID - -CLIENT_VM_2_UUID=`nova boot --flavor m1.tiny --image $DEFAULT_IMAGE_NAME --nic port-id=$CLIENT2_PORT client-vm-2 | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'` -die_if_not_set $LINENO CLIENT_VM_2_UUID "Failure launching client-vm-2" -confirm_server_active $CLIENT_VM_2_UUID - -####CHECKPOINT: No traffic flows - -# policy-rule-set Association -gbp group-update client-1 --consumed-policy-rule-sets "icmp-policy-rule-set,web-policy-rule-set" -gbp group-update client-2 --consumed-policy-rule-sets "icmp-policy-rule-set,web-policy-rule-set" -gbp group-update web --provided-policy-rule-sets "icmp-policy-rule-set,web-policy-rule-set" - -####CHECKPOINT: ICMP and HTTP work from app to web and vice versa - -gbp policy-rule-set-update web-policy-rule-set --policy-rules "secure-web-policy-rule" - -####CHECKPOINT: HTTP stops working for both the client PTGs, HTTPs is now enabled - -nova delete web-vm-1 -nova delete client-vm-1 -nova delete client-vm-2 - -if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q ACTIVE; do sleep 1; done"; then - die $LINENO "Some VMs failed to shutdown" -fi - -gbp policy-target-delete web-pt-1 -gbp policy-target-delete client-pt-1 -gbp policy-target-delete client-pt-2 - -# The following tests fixed IP assignment for PTs -WEB_SUBNET=$(gbp ptg-show web | grep subnets | awk '{print $4}') -WEB_GW_IP=$(neutron subnet-show $WEB_SUBNET | grep gateway_ip | awk '{print $4}') -FIXED_IP="${WEB_GW_IP}0" -gbp pt-create --policy-target-group web --fixed-ip subnet_id=$WEB_SUBNET,ip_address=$FIXED_IP pt-fixed-ip -neutron port-show pt_pt-fixed-ip -gbp pt-delete pt-fixed-ip - -gbp group-delete web -gbp group-delete client-1 -gbp group-delete client-2 - -gbp apg-delete myapp - -gbp policy-rule-set-delete icmp-policy-rule-set -gbp policy-rule-set-delete web-policy-rule-set - -gbp policy-rule-delete secure-web-policy-rule -gbp policy-rule-delete web-policy-rule -gbp policy-rule-delete ping-policy-rule - -gbp policy-classifier-delete secure-web-traffic -gbp policy-classifier-delete web-traffic -gbp policy-classifier-delete icmp-traffic - -gbp policy-action-delete allow - -set +o xtrace -echo "*********************************************************************" -echo "SUCCESS: End DevStack Exercise: $0" -echo "*********************************************************************" diff --git a/gbpservice/tests/contrib/devstack/exercises/gbp_fip.sh b/gbpservice/tests/contrib/devstack/exercises/gbp_fip.sh deleted file mode 100755 index b8d28902b..000000000 --- a/gbpservice/tests/contrib/devstack/exercises/gbp_fip.sh +++ /dev/null @@ -1,115 +0,0 @@ -#!/usr/bin/env bash - -# **gbp_fip.sh** - -# Sanity check that gbp fip support works if enabled - -echo "*********************************************************************" -echo "Begin DevStack Exercise: $0" -echo "*********************************************************************" - -# This script exits on an error so that errors don't compound and you see -# only the first error that occurred. -set -o errexit - -# Settings -# ======== - -# Keep track of the current directory -EXERCISE_DIR=$(cd $(dirname "$0") && pwd) -TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) - -# Import common functions -source $TOP_DIR/functions - -# Import configuration -source $TOP_DIR/openrc - -# Import exercise configuration -source $TOP_DIR/exerciserc - -source $TOP_DIR/openrc admin admin - -# Print the commands being run so that we can see the command that triggers -# an error. It is also useful for following allowing as the install occurs. -set -o xtrace - -EXT_NET_ID=$(neutron net-list --router:external=True -c id | grep -v id | awk '{print $2}' ) -EXT_NET_TO_BE_CLEANED_UP=false - -if [ -z "$EXT_NET_ID" ] ; then - EXT_NET_ID=$(neutron net-create "$PUBLIC_NETWORK_NAME" -- --router:external=True --shared | grep ' id ' | get_field 2) - EXT_SUBNET_ID=$(neutron subnet-create --ip_version 4 --gateway 172.16.73.1 --disable-dhcp --name public-subnet $EXT_NET_ID 172.16.73.0/24 | grep ' id ' | get_field 2) - EXT_NET_TO_BE_CLEANED_UP=true -else - EXT_SUBNET_ID=$(neutron subnet-show public-subnet -c id | grep id | awk '{print $4}' ) -fi - -die_if_not_set $LINENO EXT_SUBNET_ID "Failure creating external network" - -EXT_SUBNET_CIDR=$(neutron subnet-show $EXT_SUBNET_ID | grep cidr | awk '{print $4}' ) - -EXT_SUBNET_GW=$(neutron subnet-show $EXT_SUBNET_ID | grep gateway_ip | awk '{print $4}' ) - -EXT_ROUTE_DEF="--external-route destination=0.0.0.0/0,nexthop=$EXT_SUBNET_GW" -if [[ $GBP_FIP_SKIP_EXT_ROUTE ]]; then - EXT_ROUTE_DEF="" -fi -EXT_SEGMENT_ID=$(gbp external-segment-create --ip-version 4 --shared True --subnet_id=$EXT_SUBNET_ID $EXT_ROUTE_DEF --cidr $EXT_SUBNET_CIDR default | grep ' id ' | awk '{print $4}' ) - -die_if_not_set $LINENO EXT_SEGMENT_ID "Failure creating external segment" - -NAT_POOL_ID=$(gbp nat-pool-create --ip-version 4 --ip-pool $EXT_SUBNET_CIDR --external-segment $EXT_SEGMENT_ID ext_nat_pool | grep ' id ' | awk '{print $4}' ) - -die_if_not_set $LINENO NAT_POOL_ID "Failure creating nat pool" - -NSP_ID=$(gbp network-service-policy-create --network-service-params type=ip_pool,name=nat_fip,value=nat_pool nat_pool_nsp | grep ' id ' | awk '{print $4}' ) - -PTG_ID=$(gbp group-create --network-service-policy nat_pool_nsp provider_ptg | grep ' id ' | awk '{print $4}' ) - -die_if_not_set $LINENO PTG_ID "Failure creating ptg" - -PT1_ID=$(gbp policy-target-create --policy-target-group provider_ptg provider_pt1 | grep ' id ' | awk '{print $4}' ) - -die_if_not_set $LINENO PT1_ID "Failure creating policy target" - -PT2_ID=$(gbp policy-target-create --policy-target-group provider_ptg provider_pt2 | grep ' id ' | awk '{print $4}' ) - -die_if_not_set $LINENO PT2_ID "Failure creating policy target" - -PT2_PORT_ID=$(gbp policy-target-show $PT2_ID | grep ' port_id ' | awk '{print $4}' ) - -PT2_PORT_IP=$(neutron port-show $PT2_PORT_ID | grep ' fixed_ips ' | awk '{print $7}' | awk -F '"' '{print $2}' ) - -PT2_FIXED_IP=$(neutron floatingip-list | grep $PT2_PORT_IP | awk '{print $4}' ) - -die_if_not_set $LINENO PT2_FIXED_IP "Floating IP not assigned to policy target" - -PT1_PORT_ID=$(gbp policy-target-show $PT1_ID | grep ' port_id ' | awk '{print $4}' ) - -PT1_PORT_IP=$(neutron port-show $PT1_PORT_ID | grep ' fixed_ips ' | awk '{print $7}' | awk -F '"' '{print $2}' ) - -PT1_FIXED_IP=$(neutron floatingip-list | grep $PT1_PORT_IP | awk '{print $4}' ) - -die_if_not_set $LINENO PT1_FIXED_IP "Floating IP not assigned to policy target" - - - -#############Cleanup############### - - -gbp policy-target-delete $PT2_ID -gbp policy-target-delete $PT1_ID -gbp group-delete $PTG_ID -gbp network-service-policy-delete $NSP_ID -gbp nat-pool-delete $NAT_POOL_ID -gbp external-segment-delete $EXT_SEGMENT_ID - -if [ "$EXT_NET_TO_BE_CLEANED_UP" = true ] ; then - neutron net-delete $EXT_NET_ID -fi - -set +o xtrace -echo "*********************************************************************" -echo "SUCCESS: End DevStack Exercise: $0" -echo "*********************************************************************" diff --git a/gbpservice/tests/contrib/devstack/exercises/gbp_purge.sh b/gbpservice/tests/contrib/devstack/exercises/gbp_purge.sh deleted file mode 100755 index 3d698be48..000000000 --- a/gbpservice/tests/contrib/devstack/exercises/gbp_purge.sh +++ /dev/null @@ -1,110 +0,0 @@ -#!/usr/bin/env bash - -# **gbp_purge.sh** - -# Sanity check that gbp started if enabled - -echo "*********************************************************************" -echo "Begin DevStack Exercise: $0" -echo "*********************************************************************" - -# Settings -# ======== - -source functions-gbp - -# This script exits on an error so that errors don't compound and you see -# only the first error that occurred. -set -o errexit - -# Keep track of the current directory -EXERCISE_DIR=$(cd $(dirname "$0") && pwd) -TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) - -# Import common functions -source $TOP_DIR/functions - -# Import configuration -source $TOP_DIR/openrc - -# Import exercise configuration -source $TOP_DIR/exerciserc - -source $TOP_DIR/openrc demo demo - -# Print the commands being run so that we can see the command that triggers -# an error. It is also useful for following allowing as the install occurs. -set -o xtrace - -# Create servicechain related policies -gbp service-profile-create --vendor heat_based_node_driver --insertion-mode l3 --servicetype FIREWALL fw-profile -gbp servicechain-node-create firewall-node --template-file $TOP_DIR/gbp-templates/firewall-lb-servicechain/fw.template --service-profile fw-profile -gbp servicechain-spec-create firewall-spec --description spec --nodes "firewall-node" - -# NSP creation -gbp network-service-policy-create --network-service-params type=ip_single,name=vip_ip,value=self_subnet vip_ip_policy - -# Create action that can used in several rules -gbp policy-action-create allow_action --action-type allow -gbp policy-action-create redirect --action-type redirect --action-value firewall-spec - -# Create ICMP rule -gbp policy-classifier-create icmp-traffic --protocol icmp --direction bi -gbp policy-rule-create ping-policy-rule --classifier icmp-traffic --actions allow_action -#gbp policy-rule-create ping-policy-rule --classifier icmp-traffic --actions redirect - -# ICMP policy-rule-set -gbp policy-rule-set-create icmp-policy-rule-set --policy-rules ping-policy-rule - -# ====== PROJECT OPERATION ====== -# PTGs creation -gbp group-create --provided-policy-rule-sets "icmp-policy-rule-set" --consumed-policy-rule-sets "icmp-policy-rule-set" --network-service-policy vip_ip_policy web -gbp group-create web1 - -# PT creation -gbp policy-target-create web-pt-1 --policy-target-group web - -set +o xtrace -# create external network with admin priviledge -source $TOP_DIR/openrc admin admin -set -o xtrace -EXT_NET_ID=$(neutron net-create mgmt_out --router:external=True --shared | grep ' id ' | awk '{print $4}') -EXT_SUBNET_ID=$(neutron subnet-create --ip_version 4 --gateway 172.16.73.1 --disable-dhcp --name gbp-public-subnet $EXT_NET_ID 172.16.73.0/24 | grep ' id ' | awk '{print $4}') -openstack project list -DEMO_PROJECT_ID=$(openstack project show demo | grep "[^a-zA-Z_\d]id[^a-zA-Z_\d]" | awk '{print $4}') - -set +o xtrace -source $TOP_DIR/openrc demo demo -set -o xtrace - -# ES creation -gbp external-segment-create --ip-version 4 --external-route destination=0.0.0.0/0,nexthop=172.16.73.1 --subnet_id=$EXT_SUBNET_ID --cidr 50.50.50.0/24 mgmt_out - -gbp l3policy-update --external-segment mgmt_out default - -# Nat pool creation -gbp nat-pool-create --ip-version 4 --ip-pool 60.60.60.0/24 --external-segment mgmt_out ext_nat_pool - -# External policy creation -gbp external-policy-create --external-segment mgmt_out --provided-policy-rule-sets "icmp-policy-rule-set" --consumed-policy-rule-sets "icmp-policy-rule-set" ext_pol - -# purge all the resources -gbp purge $DEMO_PROJECT_ID - -PURGE_OUTPUT=$(gbp purge $DEMO_PROJECT_ID | grep 'Tenant has no supported resources') -die_if_not_set $LINENO PURGE_OUTPUT "Failure purging GBP resources" - -# delete the neutron resources too -set +o xtrace -source $TOP_DIR/openrc admin admin -set -o xtrace -neutron subnet-delete gbp-public-subnet -neutron net-delete mgmt_out - -check_residual_resources demo demo -check_residual_resources admin admin - -set +o xtrace -echo "*********************************************************************" -echo "SUCCESS: End DevStack Exercise: $0" -echo "*********************************************************************" diff --git a/gbpservice/tests/contrib/devstack/exercises/gbp_qos.sh b/gbpservice/tests/contrib/devstack/exercises/gbp_qos.sh deleted file mode 100755 index 319e32618..000000000 --- a/gbpservice/tests/contrib/devstack/exercises/gbp_qos.sh +++ /dev/null @@ -1,129 +0,0 @@ -#!/usr/bin/env bash - -# **gbp.sh** - -# Sanity check that gbp started if enabled - -echo "*********************************************************************" -echo "Begin DevStack Exercise: $0" -echo "*********************************************************************" - -# Settings -# ======== - -# This script exits on an error so that errors don't compound and you see -# only the first error that occurred. -set -o errexit - -# Keep track of the current directory -EXERCISE_DIR=$(cd $(dirname "$0") && pwd) -TOP_DIR=$(cd $EXERCISE_DIR/..; pwd) - -# Import common functions -source $TOP_DIR/functions - -# Import configuration -source $TOP_DIR/openrc - -# Import exercise configuration -source $TOP_DIR/exerciserc - -source $TOP_DIR/openrc demo demo - -# Print the commands being run so that we can see the command that triggers -# an error. It is also useful for following allowing as the install occurs. -set -o xtrace - -function confirm_server_active { - local VM_UUID=$1 - if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then - echo "server '$VM_UUID' did not become active!" - false - fi -} - -# Create allow action that can used in several rules -gbp policy-action-create allow --action-type allow - -# Create ICMP rule -gbp policy-classifier-create icmp-traffic --protocol icmp --direction bi -gbp policy-rule-create ping-policy-rule --classifier icmp-traffic --actions allow - -# ICMP policy-rule-set -gbp policy-rule-set-create icmp-policy-rule-set --policy-rules ping-policy-rule - -# ====== PROJECT OPERATION ====== -# PTGs creation -gbp group-create limited -gbp group-create unlimited - -# PT creation -PORT1=$(gbp policy-target-create port1-pt --policy-target-group limited | awk "/port_id/ {print \$4}") -PORT2=$(gbp policy-target-create port2-pt --policy-target-group limited | awk "/port_id/ {print \$4}") -PORT3=$(gbp policy-target-create port3-pt --policy-target-group unlimited | awk "/port_id/ {print \$4}") -PORT4=$(gbp policy-target-create port4-pt --policy-target-group unlimited | awk "/port_id/ {print \$4}") - -PORT1_VM_UUID=`nova boot --flavor m1.tiny --image $DEFAULT_IMAGE_NAME --nic port-id=$PORT1 port1-vm | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'` -die_if_not_set $LINENO PORT1_VM_UUID "Failure launching port1-vm" -confirm_server_active $PORT1_VM_UUID - -PORT2_VM_UUID=`nova boot --flavor m1.tiny --image $DEFAULT_IMAGE_NAME --nic port-id=$PORT2 port2-vm | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'` -die_if_not_set $LINENO PORT2_VM_UUID "Failure launching port2-vm" -confirm_server_active $PORT2_VM_UUID - -PORT3_VM_UUID=`nova boot --flavor m1.tiny --image $DEFAULT_IMAGE_NAME --nic port-id=$PORT3 port3-vm | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'` -die_if_not_set $LINENO PORT3_VM_UUID "Failure launching port3-vm" -confirm_server_active $PORT3_VM_UUID - -PORT4_VM_UUID=`nova boot --flavor m1.tiny --image $DEFAULT_IMAGE_NAME --nic port-id=$PORT4 port4-vm | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'` -die_if_not_set $LINENO PORT4_VM_UUID "Failure launching port4-vm" -confirm_server_active $PORT4_VM_UUID - -####CHECKPOINT: No traffic flows between groups and no QoS applied - -# policy-rule-set Association -gbp group-update limited --consumed-policy-rule-sets "icmp-policy-rule-set" -gbp group-update unlimited --provided-policy-rule-sets "icmp-policy-rule-set" - -####CHECKPOINT: ICMP now flows between each group, but still no QoS applied - -# Create Network Service Policy that includes QoS parameters -gbp network-service-policy-create --network-service-params type=qos_burstrate,name=qos_burstrate,value=500 --network-service-params type=qos_maxrate,name=qos_maxrate,value=8000 "qos" - -# Limit every PT in the limited PTG by associating the "qos" NSP created right before -gbp group-update limited --network-service-policy "qos" - -####CHECKPOINT: Both port1-pt and port2-pt will not be able to exceed 8 Mbps with a burst rate of 500 Kb - -nova delete port4-vm -nova delete port3-vm -nova delete port2-vm -nova delete port1-vm - -if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q ACTIVE; do sleep 1; done"; then - die $LINENO "Some VMs failed to shutdown" -fi - -gbp policy-target-delete port4-pt -gbp policy-target-delete port3-pt -gbp policy-target-delete port2-pt -gbp policy-target-delete port1-pt - -gbp group-delete unlimited -gbp group-delete limited - -gbp policy-rule-set-delete icmp-policy-rule-set - -gbp policy-rule-delete ping-policy-rule - -gbp policy-classifier-delete icmp-traffic - -gbp policy-action-delete allow - -# Delete Network Service Policy that includes QoS parameters -gbp network-service-policy-delete "qos" - -set +o xtrace -echo "*********************************************************************" -echo "SUCCESS: End DevStack Exercise: $0" -echo "*********************************************************************" diff --git a/gbpservice/tests/contrib/devstack/gbp-templates/firewall-lb-servicechain/demo.yaml b/gbpservice/tests/contrib/devstack/gbp-templates/firewall-lb-servicechain/demo.yaml deleted file mode 100644 index 30269e7b2..000000000 --- a/gbpservice/tests/contrib/devstack/gbp-templates/firewall-lb-servicechain/demo.yaml +++ /dev/null @@ -1,126 +0,0 @@ -#!highlight yaml - -heat_template_version: 2013-05-23 - -resources: - -# Create firewall service chain node - sc_fw_node: - type: OS::Neutron::ServiceChainNode - properties: - name: SvcChainFWNode - service_type: FIREWALL - config: { get_file: fw.template } - -# Create loadbalancer service chain node - sc_lb_node: - type: OS::Neutron::ServiceChainNode - properties: - name: SvcChainLBNode - service_type: LOADBALANCER - config: { get_file: lb.template } - -# Tie the services into a chain - sc_spec: - type: OS::Neutron::ServiceChainSpec - properties: - name: svc_chain_spec - nodes: - - { get_resource: sc_fw_node } - - { get_resource: sc_lb_node } - -# Create a network service policy - vip_ip_policy: - type: OS::Neutron::NetworkServicePolicy - properties: - name: vip_ip_policy - network_service_params: - - type: ip_single - name: vip_ip - value: self_subnet - -# Creating a classifier for all tcp traffic - any_tcp_classifier: - type: OS::Neutron::PolicyClassifier - properties: - name: any_tcp_classifier - protocol: tcp - direction: in - -# Creating redirect action - redirect_to_chain: - type: OS::Neutron::PolicyAction - properties: - name: redirect_to_chain - action_type: redirect - action_value: { get_resource: sc_spec } - -# Creating a policy rule set - tcp_traffic_rule: - type: OS::Neutron::PolicyRule - properties: - name: tcp_traffic_rule - policy_classifier_id: { get_resource: any_tcp_classifier } - policy_actions: [{ get_resource: redirect_to_chain }] - - tcp_rule_set: - type: OS::Neutron::PolicyRuleSet - properties: - name: tcp_rule_set - policy_rules: [{ get_resource: tcp_traffic_rule }] - child_policy_rule_sets: [] - -# Create EPGs for providers and consumers - app_ptg: - type: OS::Neutron::PolicyTargetGroup - properties: - name: app_ptg - provided_policy_rule_sets: - - policy_rule_set_id: { get_resource: tcp_rule_set } - policy_rule_set_scope: - network_service_policy_id: { get_resource: vip_ip_policy } - - user_ptg: - type: OS::Neutron::PolicyTargetGroup - depends_on: app_server_pt - properties: - name: user_ptg - consumed_policy_rule_sets: - - policy_rule_set_id: { get_resource: tcp_rule_set } - policy_rule_set_scope: - -# Create webserver - - app_server_pt: - type: OS::Neutron::PolicyTarget - properties: - name: app_server_pt - policy_target_group_id: { get_resource: app_ptg } - - app_server: - type: OS::Nova::Server - properties: - name: app_svr - image: cirros-0.3.2-x86_64-uec - flavor: m1.tiny - networks: - - port: {get_attr: [app_server_pt, port_id]} - -# Create a user - - user_server_pt: - type: OS::Neutron::PolicyTarget - properties: - name: user_server_pt - policy_target_group_id: { get_resource: user_ptg } - - user_server: - type: OS::Nova::Server - properties: - name: user_svr - image: cirros-0.3.2-x86_64-uec - flavor: m1.tiny - networks: - - port: {get_attr: [user_server_pt, port_id]} - - diff --git a/gbpservice/tests/contrib/devstack/gbp-templates/firewall-lb-servicechain/fw.template b/gbpservice/tests/contrib/devstack/gbp-templates/firewall-lb-servicechain/fw.template deleted file mode 100644 index 6d70394a8..000000000 --- a/gbpservice/tests/contrib/devstack/gbp-templates/firewall-lb-servicechain/fw.template +++ /dev/null @@ -1,36 +0,0 @@ -{ - "heat_template_version": "2013-05-23", - "resources": { - "Firewall_service": { - "type": "OS::Neutron::Firewall", - "properties": { - "admin_state_up": true, - "firewall_policy_id": {"get_resource": "Firewall_policy"}, - "name": "testFirewall", - "description": "test Firewall" - } - }, - "Firewall_policy": { - "type": "OS::Neutron::FirewallPolicy", - "properties": { - "shared": false, - "description": "test firewall policy", - "name": "testFWPolicy", - "firewall_rules": [{"get_resource": "Firewall_rule"}], - "audited": true - } - }, - "Firewall_rule": { - "type": "OS::Neutron::FirewallRule", - "properties": { - "protocol": "tcp", - "description": "firewall rule 1", - "enabled": true, - "destination_port": "80", - "shared": false, - "action": "allow", - "name": "testFw" - } - } - } -} diff --git a/gbpservice/tests/contrib/devstack/gbp-templates/firewall-lb-servicechain/lb.template b/gbpservice/tests/contrib/devstack/gbp-templates/firewall-lb-servicechain/lb.template deleted file mode 100644 index 6cc317c9c..000000000 --- a/gbpservice/tests/contrib/devstack/gbp-templates/firewall-lb-servicechain/lb.template +++ /dev/null @@ -1,59 +0,0 @@ -{ - "AWSTemplateFormatVersion" : "2010-09-09", - "Description": "Template to test Haproxy Loadbalacer service", - - "Parameters": { - "Subnet": { - "Description": "Pool Subnet CIDR, on which VIP port should be created", - "Type": "String" - }, - "vip_ip": { - "Description": "VIP IP Address", - "Type": "String" - } - }, - - "Resources" : { - "HttpHM": { - "Type": "OS::Neutron::HealthMonitor", - "Properties": { - "admin_state_up": true, - "delay": 20, - "expected_codes": "200", - "http_method": "GET", - "max_retries": 3, - "timeout": 10, - "type": "HTTP", - "url_path": "/" - } - }, - "HaproxyPool": { - "Type": "OS::Neutron::Pool", - "Properties": { - "admin_state_up": true, - "description": "Haproxy pool from teplate", - "lb_method": "ROUND_ROBIN", - "monitors": [{"Ref":"HttpHM"}], - "name": "Haproxy pool", - "protocol": "HTTP", - "subnet_id": {"Ref":"Subnet"}, - "vip": { - "subnet": {"Ref":"Subnet"}, - "address": {"Ref":"vip_ip"}, - "name": "Haproxy vip", - "protocol_port": 80, - "connection_limit": -1, - "admin_state_up": true, - "description": "Haproxy vip from template" - } - } - }, - "HaproxyLb": { - "Type": "OS::Neutron::LoadBalancer", - "Properties": { - "pool_id": {"Ref":"HaproxyPool"}, - "protocol_port": 80 - } - } - } -} diff --git a/gbpservice/tests/contrib/devstack/lib/gbp b/gbpservice/tests/contrib/devstack/lib/gbp deleted file mode 100644 index df3df6721..000000000 --- a/gbpservice/tests/contrib/devstack/lib/gbp +++ /dev/null @@ -1,66 +0,0 @@ -# lib/gbp -# functions - functions specific to group-based-policy - -# Dependencies: -# ``functions`` file -# ``DEST`` must be defined -# ``STACK_USER`` must be defined - -# ``stack.sh`` calls the entry points in this order: -# -# - install_gbp -# - install_gbpclient -# - init_gbp -# -# ``unstack.sh`` calls the entry points in this order: - -# Set up default directories -GBPSERVICE_DIR=$DEST/group-based-policy -GBPCLIENT_DIR=$DEST/python-gbpclient -GBPHEAT_DIR=$DEST/group-based-policy-automation -GBPUI_DIR=$DEST/group-based-policy-ui -NEUTRON_CONF_DIR=/etc/neutron -NEUTRON_CONF=$NEUTRON_CONF_DIR/neutron.conf - -# Save trace setting -XTRACE=$(set +o | grep xtrace) -set +o xtrace - - -# Functions -# --------- - -# init_gbpservice() - Initialize databases, etc. -function init_gbpservice { - # Run GBP db migrations - gbp-db-manage --config-file $NEUTRON_CONF --config-file /$Q_PLUGIN_CONF_FILE upgrade head -} - -# install_gbpservice() - Collect source and prepare -function install_gbpservice { - mv $GBPSERVICE_DIR/test-requirements.txt $GBPSERVICE_DIR/_test-requirements.txt - pip_install -e $GBPSERVICE_DIR - #pip_install -e $GBP_DIR - #sed -i '/gbptestneutron/d' $GBPSERVICE_DIR/test-requirements.txt - #setup_develop $GBPSERVICE_DIR - \cp -rf $GBPSERVICE_DIR/etc/policy.json $Q_POLICY_FILE - sed -i 's/"context_is_admin": "role:admin"/"context_is_admin": "role:admin or user_name:neutron"/g' $Q_POLICY_FILE - mv $GBPSERVICE_DIR/_test-requirements.txt $GBPSERVICE_DIR/test-requirements.txt -} - -# install_gbpclient() - Collect source and prepare -function install_gbpclient { - git_clone $GBPCLIENT_REPO $GBPCLIENT_DIR $GBPCLIENT_BRANCH - mv $GBPCLIENT_DIR/test-requirements.txt $GBPCLIENT_DIR/_test-requirements.txt - setup_develop $GBPCLIENT_DIR - sudo install -D -m 0644 -o $STACK_USER {$GBPCLIENT_DIR/tools/,/etc/bash_completion.d/}gbp.bash_completion - mv $GBPCLIENT_DIR/_test-requirements.txt $GBPCLIENT_DIR/test-requirements.txt -} - -# Restore xtrace -$XTRACE - -# Tell emacs to use shell-script-mode -## Local variables: -## mode: shell-script -## End: diff --git a/gbpservice/tests/contrib/devstack/local-aim.conf b/gbpservice/tests/contrib/devstack/local-aim.conf deleted file mode 100644 index bce99ed71..000000000 --- a/gbpservice/tests/contrib/devstack/local-aim.conf +++ /dev/null @@ -1,23 +0,0 @@ -[[local|localrc]] -DATABASE_PASSWORD=abc123 -ADMIN_PASSWORD=abc123 -MYSQL_PASSWORD=$DATABASE_PASSWORD -RABBIT_PASSWORD=abc123 -SERVICE_PASSWORD=$ADMIN_PASSWORD -SERVICE_TOKEN=abc123 - -LOG_COLOR=False -DEST=/opt/stack/new -LOGFILE=$DEST/logs/stack.sh.log -SKIP_EXERCISES=volumes,trove,swift,sahara,euca,bundle,boot_from_volume,aggregates,zaqar,client-env,client-args,sec_groups,neutron-adv-test,floating_ips,horizon - -enable_plugin group-based-policy https://opendev.org/x/group-based-policy.git master - -enable_plugin networking-sfc https://opendev.org/openstack/networking-sfc.git stable/xena - -ENABLE_APIC_AIM_GATE=True - -AIM_BRANCH=master -OPFLEX_BRANCH=stable/xena -APICAPI_BRANCH=master -ACITOOLKIT_BRANCH=noiro-lite diff --git a/gbpservice/tests/contrib/devstack/local-nfp.conf b/gbpservice/tests/contrib/devstack/local-nfp.conf deleted file mode 100644 index 37ed1c730..000000000 --- a/gbpservice/tests/contrib/devstack/local-nfp.conf +++ /dev/null @@ -1,130 +0,0 @@ -[[local|localrc]] -DATABASE_PASSWORD=abc123 -ADMIN_PASSWORD=abc123 -MYSQL_PASSWORD=$DATABASE_PASSWORD -RABBIT_PASSWORD=abc123 -SERVICE_PASSWORD=$ADMIN_PASSWORD -SERVICE_TOKEN=abc123 - -Q_SERVICE_PLUGIN_CLASSES=neutron.services.l3_router.l3_router_plugin.L3RouterPlugin,group_policy,ncp,qos - - -# Using group-policy branches -# --------------------------- - -GIT_BASE=${GIT_BASE:-https://opendev.org} - -GBPSERVICE_REPO=${GIT_BASE}/x/group-based-policy.git -GBPSERVICE_BRANCH=master -#GBPSERVICE_REPO=https://review.openstack.org/openstack/group-based-policy -#GBPSERVICE_BRANCH=refs/changes/85/298385/154 - -GBPCLIENT_REPO=${GIT_BASE}/x/python-group-based-policy-client.git -GBPCLIENT_BRANCH=stable/xena - -GBPUI_REPO=${GIT_BASE}/x/group-based-policy-ui.git -GBPUI_BRANCH=master -#GBPUI_REPO=https://review.openstack.org/openstack/group-based-policy-ui -#GBPUI_BRANCH=refs/changes/02/136802/14 - -GBPHEAT_REPO=${GIT_BASE}/x/group-based-policy-automation.git -GBPHEAT_BRANCH=master -#GBPHEAT_REPO= -#GBPHEAT_BRANCH= - -# Enable neutron for group-policy-poc -# ----------------------------------- -disable_service n-net -#disable_service h-eng -#disable_service h-api -#disable_service h-api-cfn -#disable_service h-api-cw -enable_service q-svc -enable_service q-agt -enable_service q-dhcp -enable_service q-l3 -enable_plugin neutron-fwaas https://opendev.org/openstack/neutron-fwaas.git stable/xena -enable_plugin neutron-lbaas https://opendev.org/openstack/neutron-lbaas.git stable/xena -enable_plugin neutron https://opendev.org/openstack/neutron.git stable/xena - - -#ENBALE OCTAVIA -enable_plugin octavia https://opendev.org/openstack/octavia stable/xena -#ENABLED_SERVICES+=,octavia,o-cw,o-hk,o-hm,o-api - -enable_service q-fwaas-v1 -enable_service q-lbaasv2 -enable_service q-meta -enable_service neutron -enable_service group-policy -disable_service tempest -ENABLED_SERVICES+=,heat,h-api,h-api-cfn,h-api-cw,h-eng -enable_service orchestrator,proxy,proxy_agent,base_configurator,q-lbaasv2-agent - -LOG_COLOR=False -DEST=/opt/stack/new -LOGFILE=$DEST/logs/stack.sh.log -SKIP_EXERCISES=volumes,trove,swift,sahara,euca,bundle,boot_from_volume,aggregates,zaqar,client-env,client-args,sec_groups,neutron-adv-test,floating_ips,horizon - -#OFFLINE=True -RECLONE=True - -# Group-based Policy configuration -# Comment the lines below if you don't want to configure the datapath -# and use the dummy driver. -[[post-config|/etc/nova/nova.conf]] -[neutron] -allow_duplicate_networks = True - -#[[post-config|/etc/heat/heat.conf]] -#[DEFAULT] -#plugin_dirs=/opt/stack/gbpautomation/gbpautomation/heat - -[[post-config|/etc/neutron/neutron.conf]] -[keystone_authtoken] -project_name = service -username = neutron -password = abc123 - -[group_policy] -policy_drivers=implicit_policy,resource_mapping,chain_mapping -extension_drivers=proxy_group - -[servicechain] -servicechain_drivers = simplechain_driver -#servicechain_drivers = chain_with_two_arm_appliance_driver - -[node_composition_plugin] -node_plumber = admin_owned_resources_apic_plumber -node_drivers = heat_node_driver,nfp_node_driver - -[admin_owned_resources_apic_tscp] -plumbing_resource_owner_user = neutron -plumbing_resource_owner_password = abc123 -plumbing_resource_owner_tenant_name = service - -[group_policy_implicit_policy] -default_ip_pool = 10.0.0.0/8 -default_proxy_ip_pool = 192.169.0.0/16 -default_external_segment_name = default - -[nfp_node_driver] -is_service_admin_owned = False -svc_management_ptg_name = svc_management_ptg - -[quotas] -default_quota = -1 -quota_network = -1 -quota_subnet = -1 -quota_port = -1 -quota_security_group = -1 -quota_security_group_rule = -1 -quota_router = -1 -quota_floatingip = -1 - -[agent] -extensions = qos - -[[post-config|/etc/neutron/plugins/ml2/ml2_conf.ini]] -[ml2] -extension_drivers = qos,port_security diff --git a/gbpservice/tests/contrib/devstack/local.conf b/gbpservice/tests/contrib/devstack/local.conf deleted file mode 100644 index 5f8eff093..000000000 --- a/gbpservice/tests/contrib/devstack/local.conf +++ /dev/null @@ -1,103 +0,0 @@ -[[local|localrc]] -DATABASE_PASSWORD=abc123 -ADMIN_PASSWORD=abc123 -MYSQL_PASSWORD=$DATABASE_PASSWORD -RABBIT_PASSWORD=abc123 -SERVICE_PASSWORD=$ADMIN_PASSWORD -SERVICE_TOKEN=abc123 - -Q_SERVICE_PLUGIN_CLASSES=neutron.services.l3_router.l3_router_plugin.L3RouterPlugin,group_policy,ncp,qos - - -# Using group-policy branches -# --------------------------- - -GIT_BASE=${GIT_BASE:-https://opendev.org} - -#GBPSERVICE_REPO=${GIT_BASE}/x/group-based-policy.git -GBPSERVICE_BRANCH=master -#GBPSERVICE_REPO=https://review.openstack.org/openstack/group-based-policy -#GBPSERVICE_BRANCH=refs/changes/54/240954/47 - -GBPCLIENT_REPO=${GIT_BASE}/x/python-group-based-policy-client.git -GBPCLIENT_BRANCH=stable/xena - -GBPUI_REPO=${GIT_BASE}/x/group-based-policy-ui.git -GBPUI_BRANCH=master -#GBPUI_REPO=https://review.openstack.org/openstack/group-based-policy-ui -#GBPUI_BRANCH=refs/changes/02/136802/14 - -GBPHEAT_REPO=${GIT_BASE}/x/group-based-policy-automation.git -GBPHEAT_BRANCH=master -#GBPHEAT_REPO= -#GBPHEAT_BRANCH= - -# Enable neutron for group-policy-poc -# ----------------------------------- -disable_service n-net -#disable_service h-eng -#disable_service h-api -#disable_service h-api-cfn -#disable_service h-api-cw -enable_service q-svc -enable_service q-agt -enable_service q-dhcp -enable_service q-l3 -enable_service q-meta -enable_service neutron -enable_service group-policy -disable_service tempest -ENABLED_SERVICES+=,heat,h-api,h-api-cfn,h-api-cw,h-eng - -# The following should be uncommented for FWAAS -# enable_plugin neutron-fwaas http://opendev.org/openstack/neutron-fwaas.git -# enable_service q-fwaas-v1 - -LOG_COLOR=False -DEST=/opt/stack/new -LOGFILE=$DEST/logs/stack.sh.log -SKIP_EXERCISES=volumes,trove,swift,sahara,euca,bundle,boot_from_volume,aggregates,zaqar,client-env,client-args,sec_groups,neutron-adv-test,floating_ips,horizon - -#OFFLINE=True -RECLONE=True - -# Group-based Policy configuration -# Comment the lines below if you don't want to configure the datapath -# and use the dummy driver. -[[post-config|/etc/nova/nova.conf]] -[neutron] -allow_duplicate_networks = True - -#[[post-config|/etc/heat/heat.conf]] -#[DEFAULT] -#plugin_dirs=/opt/stack/gbpautomation/gbpautomation/heat - -[[post-config|/etc/neutron/neutron.conf]] -[group_policy] -policy_drivers=implicit_policy,resource_mapping,chain_mapping -extension_drivers=proxy_group - -[servicechain] -servicechain_drivers = simplechain_driver -#servicechain_drivers = chain_with_two_arm_appliance_driver - -[node_composition_plugin] -node_plumber = stitching_plumber -node_drivers = heat_node_driver - -[quotas] -default_quota = -1 -quota_network = -1 -quota_subnet = -1 -quota_port = -1 -quota_security_group = -1 -quota_security_group_rule = -1 -quota_router = -1 -quota_floatingip = -1 - -[agent] -extensions = qos - -[[post-config|/etc/neutron/plugins/ml2/ml2_conf.ini]] -[ml2] -extension_drivers = qos diff --git a/gbpservice/tests/contrib/devstack/nfp b/gbpservice/tests/contrib/devstack/nfp deleted file mode 100644 index 97182b4a0..000000000 --- a/gbpservice/tests/contrib/devstack/nfp +++ /dev/null @@ -1,326 +0,0 @@ -#!/bin/bash -NEW_BASE="$BASE/new" -DISKIMAGE_CREATE_DIR=$NEW_BASE/group-based-policy/gbpservice/contrib/nfp/tools/image_builder/ - -function prepare_nfp_image_builder { - #setup_develop $NFPSERVICE_DIR - sudo -H -E pip install -r $DISKIMAGE_CREATE_DIR/requirements.txt - sudo apt-get install -y --force-yes qemu-utils -} - -function create_nfp_image { - TOP_DIR=$1 - sudo python -c\ - 'from gbpservice.contrib.nfp.tools.image_builder import disk_image_create as DIB;\ - DIB.cur_dir = "'$DISKIMAGE_CREATE_DIR'";\ - DIB.conf["ubuntu_release"] = {"release": "wily"};\ - DIB.conf["dib"] = {"image_size": 3, "elements": ["nfp-reference-configurator", "dhcp-all-interfaces", "devuser"], "offline": True, "cache_dir": "'$HOME'/.cache/image-create"};\ - DIB.dib()' - BUILT_IMAGE_PATH=$(cat $DISKIMAGE_CREATE_DIR/output/last_built_image_path) - upload_image file://$BUILT_IMAGE_PATH - - openstack --os-cloud=devstack-admin flavor create --ram 512 --disk 3 --vcpus 1 m1.nfp-tiny -} - -function assign_user_role_credential { - openstack --os-cloud=devstack-admin role add --project service --user nova service - openstack --os-cloud=devstack-admin role add --project service --user neutron admin -} - -function namespace_delete { - TOP_DIR=$1 - source $TOP_DIR/openrc neutron service - #Deletion namespace - NFP_P=`sudo ip netns | grep "nfp-proxy"` - if [ ${#NFP_P} -ne 0 ]; then - sudo ip netns delete nfp-proxy - echo "namespace removed" - fi - - #Delete veth peer - PEER=`ip a | grep pt1` - if [ ${#PEER} -ne 0 ]; then - echo "veth peer removed" - sudo ip link delete pt1 - fi - - #pt1 port removing from ovs - PORT=`sudo ovs-vsctl show | grep "pt1"` - if [ ${#PORT} -ne 0 ]; then - sudo ovs-vsctl del-port br-int pt1 - echo "ovs port ptr1 is removed" - fi - - echo "nfp-proxy cleaning success.... " -} - -function namespace_create { - TOP_DIR=$1 - #doing it in namespace_delete, so no need to do it again - #source $1/openrc neutron service - SERVICE_MGMT_NET="l2p_svc_management_ptg" - cidr="/24" - echo "Creating new namespace nfp-proxy...." - - #new namespace with name proxy - NFP_P=`sudo ip netns add nfp-proxy` - if [ ${#NFP_P} -eq 0 ]; then - echo "New namepace nfp-proxt create" - else - echo "nfp-proxy creation failed" - exit 0 - fi - - #Create veth peer - PEER=`sudo ip link add pt0 type veth peer name pt1` - if [ ${#PEER} -eq 0 ]; then - echo "New veth pair created" - else - echo "veth pair creation failed" - exit 0 - fi - sleep 1 - - #move one side of veth into namesape - sudo ip link set pt0 netns nfp-proxy - - #create new neutron port in service mgmt network - new_ip=`neutron port-create --name nfp-proxy_port $SERVICE_MGMT_NET | grep "fixed_ips" | awk '{print $7}' | sed 's/^\"\(.*\)\"}$/\1/'` - if [ ${#new_ip} -lt 5 ]; then - echo "new_ip =$new_ip" - echo "Neutron port creation failed (check source) " - exit 0 - else - echo "New Neutron Port Created on Service management network with ip =$new_ip" - fi - new_ip_cidr+="$new_ip/24" - sleep 2 - - #get the ip address of new port eg : 11.0.0.6 and asign to namespace - sudo ip netns exec nfp-proxy ip addr add $new_ip_cidr dev pt0 - - #move other side of veth into ovs : br-int - sudo ovs-vsctl add-port br-int pt1 - - #get id of service management network - smn_id=`neutron net-list | grep "$SERVICE_MGMT_NET" | awk '{print $2}'` - - #get the dhcp namespace of service management network - nm_space=`sudo ip netns | grep "$smn_id"` - - #get port id from router nampace - port=`sudo ip netns exec $nm_space ip a | grep "tap" | tail -n 1 | awk '{print $7}'` - - #get tag_id form port in ovs-bridge - tag_id=`sudo ovs-vsctl list port $port | grep "tag" | tail -n 1 | awk '{print $3}'` - - sudo ovs-vsctl set port pt1 tag=$tag_id - - #up the both ports - sudo ip netns exec nfp-proxy ip link set pt0 up - sudo ip netns exec nfp-proxy ip link set lo up - sudo ip link set pt1 up - - PING=`sudo ip netns exec nfp-proxy ping $2 -q -c 2 > /dev/null` - if [ ${#PING} -eq 0 ] - then - echo "nfp-proxy namespcace creation success and reaching to $2" - else - echo "Fails reaching to $2" - fi - - sudo ip netns exec nfp-proxy /usr/bin/nfp_proxy --config-file=/etc/nfp.ini -} - -function create_nfp_gbp_resources { - TOP_DIR=$1 - source $TOP_DIR/openrc neutron service - IMAGE_PATH=$(cat $DISKIMAGE_CREATE_DIR/output/last_built_image_path) - IMAGE_NAME=`basename "$IMAGE_PATH"` - IMAGE_NAME_FLAT="${IMAGE_NAME%.*}" - FLAVOR=m1.nfp-tiny - - gbp network-service-policy-create --network-service-params type=ip_pool,name=vip_ip,value=nat_pool svc_mgmt_fip_policy - gbp service-profile-create --servicetype LOADBALANCERV2 --insertion-mode l3 --shared True --service-flavor service_vendor=haproxy,device_type=None --vendor NFP base_mode_lb - gbp service-profile-create --servicetype FIREWALL --insertion-mode l3 --shared True --service-flavor service_vendor=vyos,device_type=None --vendor NFP base_mode_fw - gbp service-profile-create --servicetype FIREWALL --insertion-mode l3 --shared True --service-flavor service_vendor=nfp,device_type=nova,image_name=$IMAGE_NAME_FLAT,flavor=$FLAVOR --vendor NFP base_mode_fw_vm - - gbp l3policy-create --ip-version 4 --ip-pool 172.16.0.0/16 --subnet-prefix-length 20 --proxy-ip-pool=172.17.0.0/16 service_management - - gbp l2policy-create --l3-policy service_management svc_management_ptg - - gbp group-create svc_management_ptg --service_management True --l2-policy svc_management_ptg - -} - -function delete_nfp_gbp_resources { - TOP_DIR=$1 - source $TOP_DIR/openrc neutron service - - neutron port-delete nfp-proxy_port - - gbp ptg-show svc_management_ptg -f value -c policy_targets - gbp ptg-show svc_management_ptg -f value -c policy_targets | xargs -I {} gbp pt-show {} - nova list - - gbp group-delete svc_management_ptg - gbp service-profile-delete base_mode_fw_vm - gbp service-profile-delete base_mode_fw - gbp service-profile-delete base_mode_lb - gbp network-service-policy-delete svc_mgmt_fip_policy -} - -function get_router_namespace { - TOP_DIR=$1 - source $TOP_DIR/openrc neutron service - - GROUP="svc_management_ptg" - echo "GroupName: $GROUP" - - l2p_id=`gbp ptg-show svc_management_ptg | grep l2_policy_id | awk '{print $4}'` - l3p_id=`gbp l2p-show $l2p_id | grep l3_policy_id | awk '{print $4}'` - RouterId=`gbp l3p-show $l3p_id | grep routers | awk '{print $4}'` -} - -function copy_nfp_files_and_start_process { - TOP_DIR=$1 - cd $NEW_BASE/group-based-policy/gbpservice/nfp - sudo cp -r bin/nfp /usr/bin/ - sudo chmod +x /usr/bin/nfp - sudo rm -rf /etc/nfp.ini - sudo cp -r bin/nfp.ini /etc/ - sudo cp -r bin/nfp_proxy /usr/bin/ - - configurator_ip=127.0.0.1 - configurator_port=8080 - echo "Configuring nfp.ini .... with nfp_controller_ip as $configurator_ip" - sudo sed -i "s/nfp_controller_ip=*.*/nfp_controller_ip=$configurator_ip/g" /etc/nfp.ini - sudo sed -i "s/nfp_controller_port= *.*/nfp_controller_port=$configurator_port/g" /etc/nfp.ini - - source $TOP_DIR/inc/ini-config - - admin_user=`iniget /etc/neutron/neutron.conf keystone_authtoken username` - admin_password=`iniget /etc/neutron/neutron.conf keystone_authtoken password` - admin_tenant_name=`iniget /etc/neutron/neutron.conf keystone_authtoken project_name` - auth_uri=`iniget /etc/neutron/neutron.conf keystone_authtoken auth_uri` - auth_protocol=$(echo $auth_uri | tr ':/' ' ' | awk '{print $1}') - auth_host=$(echo $auth_uri | tr ':/' ' ' | awk '{print $2}') - #auth_port=$(echo $auth_uri | tr ':/' ' ' | awk '{print $3}') - #auth_version=$(echo $auth_uri | tr ':/' ' ' | awk '{print $4}') - #auth_version=${auth_version:-v2.0} - auth_port=5000 - auth_version=v2.0 - iniset -sudo /etc/nfp.ini nfp_keystone_authtoken admin_user $admin_user - iniset -sudo /etc/nfp.ini nfp_keystone_authtoken admin_password $admin_password - iniset -sudo /etc/nfp.ini nfp_keystone_authtoken admin_tenant_name $admin_tenant_name - iniset -sudo /etc/nfp.ini nfp_keystone_authtoken auth_protocol $auth_protocol - iniset -sudo /etc/nfp.ini nfp_keystone_authtoken auth_host $auth_host - iniset -sudo /etc/nfp.ini nfp_keystone_authtoken auth_port $auth_port - iniset -sudo /etc/nfp.ini nfp_keystone_authtoken auth_version $auth_version - - ipnetns_router=`sudo ip netns |grep $RouterId` - - source $TOP_DIR/functions-common - - echo "Starting orchestrator >>>> under screen named : orchestrator" - run_process orchestrator "sudo /usr/bin/nfp --module orchestrator --config-file /etc/neutron/neutron.conf --config-file /etc/neutron/plugins/ml2/ml2_conf.ini --config-file /etc/nfp.ini --log-file $DEST/logs/nfp_orchestrator.log" - sleep 1 - - echo "Starting proxy_agent >>>> under screen named : proxy_agent" - run_process proxy_agent "sudo /usr/bin/nfp --module proxy_agent --config-file /etc/nfp.ini --log-file $DEST/logs/nfp_proxy_agent.log" - sleep 1 - - echo "Starting proxy server under Namespace : nfp-proxy namespace >>>> under screen named : proxy" - run_process proxy "source $NEW_BASE/devstack/lib/nfp;namespace_delete $TOP_DIR;namespace_create $TOP_DIR $configurator_ip" - sleep 10 - - cd pecan/api - sudo python setup.py develop - sudo mkdir -p /var/log/nfp - sudo touch /var/log/nfp/nfp_pecan.log - echo "Starting base_configurator >>>> under screen named : base_configurator" - run_process base_configurator "cd $NEW_BASE/group-based-policy/gbpservice/nfp/pecan/api;sudo ip netns exec nfp-proxy pecan configurator_decider config.py --mode base" - sleep 1 - - echo "Running gbp-db-manage" - - source $TOP_DIR/openrc neutron service - - gbp-db-manage --config-file /etc/neutron/neutron.conf upgrade head - sleep 2 - echo "Configuration success ... " -} - - -function restart_devstack_screen_processes { - SCREEN_NAME=stack - SERVICE_DIR=$DEST/status/$SCREEN_NAME - bin=/usr/local/bin - proc_screen_name=$1 - sandbox=$2 - proc_name=$3 - param=$4 - extra_param=$5 - - cmd=$bin/$proc_name\ $param\ $extra_param - cmd="$(echo -e "${cmd}" | sed -e 's/[[:space:]]*$//')" - - if [[ ! -z "${sandbox// }" ]]; then - cmd=$sandbox\ \'$cmd\' - fi - - # stop the process - screen -S $SCREEN_NAME -p $proc_screen_name -X kill - sleep 4 - - # start the process - screen -S $SCREEN_NAME -X screen -t $proc_screen_name - screen -S $SCREEN_NAME -p $proc_screen_name -X stuff "$cmd \ - & echo \$! >$SERVICE_DIR/${proc_screen_name}.pid; fg || \ - echo \"$proc_screen_name failed to start\" \ - | tee \"$SERVICE_DIR/${proc_screen_name}.failure\"\n" - sleep 5 -} - -function restart_neutron_server { - proc=q-svc - proc_name=neutron-server - sandbox= - param="--config-file /etc/neutron/neutron.conf --config-file /etc/neutron/plugins/ml2/ml2_conf.ini" - extra_param="--log-file /opt/stack/new/logs/q-svc.log" - restart_devstack_screen_processes "$proc" "$sandbox" "$proc_name" "$param" "$extra_param" -} - -function configure_lbaas { - echo "Configuring NFP Loadbalancer plugin driver" - LBAAS_SERVICE_PROVIDER=LOADBALANCERV2:Haproxy:neutron_lbaas.drivers.haproxy.plugin_driver.HaproxyOnHostPluginDriver:default - sudo\ - sed\ - -i\ - '/^service_provider.*:default/'\ -'s'/\ -':default'/\ -'\n'\ -"service_provider = $LBAAS_SERVICE_PROVIDER"/\ - /etc/neutron/neutron_lbaas.conf - - echo "Configuring ineterface driver" - iniset -sudo /etc/neutron/neutron_lbaas.conf DEFAULT interface_driver openvswitch - echo "whereis neutron-lbaasv2-agent: " `whereis neutron-lbaasv2-agent` - TOP_DIR=$1 - source $TOP_DIR/functions-common - echo "Starting Lbaasv2 Agent" - run_process q-lbaasv2-agent "sudo neutron-lbaasv2-agent --config-file /etc/neutron/neutron.conf --config-file /etc/neutron/neutron_lbaas.conf" - - restart_neutron_server -} - -function nfp_setup { - prepare_nfp_image_builder - create_nfp_image $1 - assign_user_role_credential $1 - create_nfp_gbp_resources $1 - get_router_namespace $1 - configure_lbaas $1 - copy_nfp_files_and_start_process $1 -} diff --git a/gbpservice/tests/contrib/devstack/nfp-templates/fw_template.yml b/gbpservice/tests/contrib/devstack/nfp-templates/fw_template.yml deleted file mode 100644 index 24f1b9509..000000000 --- a/gbpservice/tests/contrib/devstack/nfp-templates/fw_template.yml +++ /dev/null @@ -1,45 +0,0 @@ -heat_template_version: 2013-05-23 - -description: Template to deploy firewall - -resources: - sc_firewall: - type: OS::Neutron::Firewall - - properties: - description: "{'insert_type': 'east_west', 'vm_management_ip': u'192.168.20.138', 'provider_ptg_info': ['fa:16:3e:28:7d:b2']}" - - firewall_policy_id: { get_resource: sc_firewall_policy } - name: "serviceVM_infra_FW" - - sc_firewall_policy: - type: OS::Neutron::FirewallPolicy - properties: - name: "" - firewall_rules: [{ get_resource: sc_firewall_rule1 } , { get_resource: sc_firewall_rule2 }, { get_resource: sc_firewall_rule3 },{get_resource: sc_firewall_rule4 }] - sc_firewall_rule1: - type: OS::Neutron::FirewallRule - properties: - name: "Rule_1" - action: "allow" - protocol: "udp" - sc_firewall_rule2: - type: OS::Neutron::FirewallRule - properties: - name: "Rule_2" - action: "allow" - protocol: "icmp" - sc_firewall_rule3: - type: OS::Neutron::FirewallRule - properties: - name: "Rule_3" - action: "allow" - protocol: "tcp" - destination_port: "22" - sc_firewall_rule4: - type: OS::Neutron::FirewallRule - properties: - name: "Rule_4" - action: "allow" - protocol: "tcp" - destination_port: "80" diff --git a/gbpservice/tests/contrib/devstack/nfp-templates/fw_updated_template.yml b/gbpservice/tests/contrib/devstack/nfp-templates/fw_updated_template.yml deleted file mode 100644 index 2e5b5a3d8..000000000 --- a/gbpservice/tests/contrib/devstack/nfp-templates/fw_updated_template.yml +++ /dev/null @@ -1,31 +0,0 @@ -heat_template_version: 2013-05-23 - -description: Template to deploy firewall - -resources: - sc_firewall: - type: OS::Neutron::Firewall - - properties: - description: "{'insert_type': 'east_west', 'vm_management_ip': u'192.168.20.138', 'provider_ptg_info': ['fa:16:3e:28:7d:b2']}" - - firewall_policy_id: { get_resource: sc_firewall_policy } - name: "serviceVM_infra_FW" - - sc_firewall_policy: - type: OS::Neutron::FirewallPolicy - properties: - name: "" - firewall_rules: [{ get_resource: sc_firewall_rule1 } , { get_resource: sc_firewall_rule2 }] - sc_firewall_rule1: - type: OS::Neutron::FirewallRule - properties: - name: "Rule_1" - action: "allow" - protocol: "udp" - sc_firewall_rule2: - type: OS::Neutron::FirewallRule - properties: - name: "Rule_2" - action: "allow" - protocol: "icmp" diff --git a/gbpservice/tests/contrib/devstack/nfp-templates/haproxy.template b/gbpservice/tests/contrib/devstack/nfp-templates/haproxy.template deleted file mode 100644 index 31f381034..000000000 --- a/gbpservice/tests/contrib/devstack/nfp-templates/haproxy.template +++ /dev/null @@ -1,65 +0,0 @@ -{ - "AWSTemplateFormatVersion" : "2010-09-09", - "Description": "Template to test Haproxy Loadbalacer service", - - "Parameters": { - "Subnet": { - "Description": "Pool Subnet CIDR, on which VIP port should be created", - "Type": "String" - }, - "vip_ip": { - "Description": "VIP IP Address", - "Type": "String" - }, - "service_chain_metadata": { - "Description": "sc metadata", - "Type": "String" - } - }, - - "Resources" : { - "HttpHM": { - "Type": "OS::Neutron::HealthMonitor", - "Properties": { - "admin_state_up": true, - "delay": 20, - "expected_codes": "200", - "http_method": "GET", - "max_retries": 3, - "timeout": 10, - "type": "HTTP", - "url_path": "/" - } - }, - "HaproxyPool": { - "Type": "OS::Neutron::Pool", - "Properties": { - "admin_state_up": true, - "description": "Haproxy pool from teplate", - "lb_method": "ROUND_ROBIN", - "monitors": [{"Ref":"HttpHM"}], - "name": "Haproxy pool", - "provider": "haproxy", - "protocol": "HTTP", - "subnet_id": {"Ref":"Subnet"}, - "vip": { - "subnet": {"Ref":"Subnet"}, - "address": {"Ref":"vip_ip"}, - "name": "Haproxy vip", - "description": {"Ref":"service_chain_metadata"}, - "protocol_port": 80, - "connection_limit": -1, - "admin_state_up": true - } - } - }, - "HaproxyLb": { - "Type": "OS::Neutron::LoadBalancer", - "Properties": { - "pool_id": {"Ref":"HaproxyPool"}, - "protocol_port": 80 - } - } - } -} - diff --git a/gbpservice/tests/contrib/devstack/nfp-templates/haproxy_lbaasv2.template b/gbpservice/tests/contrib/devstack/nfp-templates/haproxy_lbaasv2.template deleted file mode 100644 index ebae76027..000000000 --- a/gbpservice/tests/contrib/devstack/nfp-templates/haproxy_lbaasv2.template +++ /dev/null @@ -1,83 +0,0 @@ -{ - "heat_template_version": "2015-10-15", - "description": "Configuration for Haproxy Neutron Loadbalacer V2 service", - "parameters": { - "lb_port": { - "type": "number", - "default": 80, - "description": "Port used by the listener" - }, - "app_port": { - "type": "number", - "default": 80, - "description": "Port used by the servers in pool" - }, - "Subnet": { - "type": "string", - "description": "Subnet on which the load balancer will be located" - }, - "vip_ip": { - "type": "string", - "description": "VIP IP Address" - }, - "service_chain_metadata": { - "type": "string", - "description": "sc metadata" - } - }, - "resources": { - "monitor": { - "type": "OS::Neutron::LBaaS::HealthMonitor", - "depends_on": "pool", - "properties": { - "delay": 3, - "type": "HTTP", - "timeout": 3, - "max_retries": 3, - "pool": { - "get_resource": "pool" - } - } - }, - "pool": { - "type": "OS::Neutron::LBaaS::Pool", - "depends_on": "listener", - "description": "{'app_port_param_name': 'app_port'}", - "properties": { - "lb_algorithm": "ROUND_ROBIN", - "protocol": "HTTP", - "listener": { - "get_resource": "listener" - } - } - }, - "listener": { - "type": "OS::Neutron::LBaaS::Listener", - "depends_on": "loadbalancer", - "properties": { - "loadbalancer": { - "get_resource": "loadbalancer" - }, - "protocol": "HTTP", - "protocol_port": { - "get_param": "lb_port" - } - } - }, - "loadbalancer": { - "type": "OS::Neutron::LBaaS::LoadBalancer", - "properties": { - "vip_subnet": { - "get_param": "Subnet" - }, - "provider": "haproxy", - "vip_address": { - "get_param": "vip_ip" - }, - "description": { - "get_param": "service_chain_metadata" - } - } - } - } -} diff --git a/gbpservice/tests/contrib/functions-gbp b/gbpservice/tests/contrib/functions-gbp deleted file mode 100644 index f3dcb1fc4..000000000 --- a/gbpservice/tests/contrib/functions-gbp +++ /dev/null @@ -1,151 +0,0 @@ -#!/usr/bin/env bash - -TOP_DIR="$BASE/new/devstack" -NEW_BASE="$BASE/new" -SCRIPTS_DIR="/usr/os-testr-env/bin" -LOGS_DIR="$NEW_BASE/logs" -ARCHIVE_LOGS_DIR="$BASE/logs" -LOCAL_CONF=$TOP_DIR/local.conf - -# The following config is legacy and is used for GBP non-devstack plugin -# install -GBP_DIR="$NEW_BASE/group-based-policy" -GBP_FUNC_DIR=$GBP_DIR/gbpservice/tests/contrib/gbpfunctests - -XTRACE=$(set +o | grep xtrace) - -function prepare_gbp_devstack_pre { - cd $TOP_DIR - sudo git checkout stable/xena - sudo sed -i 's/DEST=\/opt\/stack/DEST=\/opt\/stack\/new/g' $TOP_DIR/stackrc - sudo sed -i 's/source $TOP_DIR\/lib\/neutron/source $TOP_DIR\/lib\/neutron\nsource $TOP_DIR\/lib\/neutron-legacy/g' $TOP_DIR/stack.sh -} - -function prepare_gbp_devstack_post { - # The following should updated when master moves to a new release - # We need to do the following since the infra job clones these repos and - # checks out the master branch (as this is the master branch) and later - # does not switch to the stable/xena branch when installing devstack - # since the repo is already present. - # This can be worked around by changing the job description in - # project-config to set BRANCH_OVERRIDE to use the stable/xena branch - sudo git --git-dir=/opt/stack/new/neutron/.git --work-tree=/opt/stack/new/neutron checkout stable/xena - sudo git --git-dir=/opt/stack/new/nova/.git --work-tree=/opt/stack/new/nova checkout stable/xena - sudo git --git-dir=/opt/stack/new/keystone/.git --work-tree=/opt/stack/new/keystone checkout stable/xena - sudo git --git-dir=/opt/stack/new/cinder/.git --work-tree=/opt/stack/new/cinder checkout stable/xena - sudo git --git-dir=/opt/stack/new/requirements/.git --work-tree=/opt/stack/new/requirements checkout stable/xena - - source $TOP_DIR/functions - source $TOP_DIR/functions-common -} - -function append_to_localconf { - # REVISIT: Instead of using the subnet directly here, ideally we - # should be doing something similar to what is being done in here: - # https://github.com/openstack/neutron/commit/96a821c48b7de0017d00c43a3f9da91585873354 - sudo sed -i "$ a IPV4_ADDRS_SAFE_TO_USE=10.1.0.0/20" $TOP_DIR/local.conf -} - -function prepare_gbp_devstack { - prepare_gbp_devstack_pre - sudo mv $GBP_DIR/devstack/local-gbp.conf $TOP_DIR/local.conf - append_to_localconf - sudo cp -r $CONTRIB_DIR/devstack/gbp-templates $TOP_DIR - sudo cp $CONTRIB_DIR/devstack/exercises/*.sh $TOP_DIR/exercises/ - prepare_gbp_devstack_post -} - -function prepare_nfp_devstack { - cd $TOP_DIR - sudo cp $CONTRIB_DIR/devstack/local-nfp.conf $TOP_DIR/local.conf - append_to_localconf - sudo rm -rf $TOP_DIR/exercises/*.sh - sudo cp $CONTRIB_DIR/devstack/exercises-nfp/*.sh $TOP_DIR/exercises/ - #sudo cp $CONTRIB_DIR/devstack/exercises-nfp/lb.sh $TOP_DIR/exercises/ - sudo cp -r $CONTRIB_DIR/devstack/nfp-templates $TOP_DIR - sudo cp $CONTRIB_DIR/devstack/nfp $TOP_DIR/lib/ - sed -i 's/source $TOP_DIR\/lib\/dstat/source $TOP_DIR\/lib\/dstat\nsource $TOP_DIR\/lib\/nfp/g' stack.sh - sed -i 's/# Restore\/close logging file descriptors/nfp_setup $TOP_DIR\n# Restore\/close logging file descriptors/g' stack.sh - source $TOP_DIR/functions - source $TOP_DIR/functions-common -} - -function prepare_gbp_aim_devstack { - prepare_gbp_devstack_pre - sudo cp $CONTRIB_DIR/devstack/local-aim.conf $TOP_DIR/local.conf - append_to_localconf - sudo cp $CONTRIB_DIR/devstack/exercises-aim/gbp_aim.sh $TOP_DIR - sudo cp $CONTRIB_DIR/devstack/exercises-aim/neutron_aim.sh $TOP_DIR - # Use the aim version of the shared PRS test - sudo mv $GBP_FUNC_DIR/testcases/tc_gbp_prs_pr_shared_func.py.aim $GBP_FUNC_DIR/testcases/tc_gbp_prs_pr_shared_func.py - sudo mv $GBP_FUNC_DIR/testcases/tc_gbp_prs_func.py.aim $GBP_FUNC_DIR/testcases/tc_gbp_prs_func.py - export ENABLED_SERVICES="key,rabbit,mysql" - prepare_gbp_devstack_post -} - -function source_creds { - local xtrace=$(set +o | grep xtrace) - set +o xtrace - source $1 $2 $3 - $xtrace - -} - -function run_exercises { - $TOP_DIR/gbp_aim.sh - $TOP_DIR/neutron_aim.sh -} - -function run_gbp_rally { - # REVISIT: Temporarily disabling this job until its updated to run with Ocata - exit 1 - cd $NEW_BASE - git clone http://github.com/group-policy/rally.git -b dev-ocata - cd rally - sed -i 's/\"times\"\: 1/\"times\"\: 10/g' samples/tasks/scenarios/gbp/gbp.json - sed -i 's/\"concurrency\"\: 1/\"concurrency\"\: 10,/g' samples/tasks/scenarios/gbp/gbp.json - ./install_rally.sh -f - . /home/jenkins/rally/bin/activate - source_creds $TOP_DIR/openrc admin admin - rally deployment create --fromenv --name=gbp - rally task start samples/tasks/scenarios/gbp/gbp.json - rally task results > $LOGS_DIR/rally-task-results.log.20 - deactivate -} - -# Prepare the log files for Jenkins to upload -function prepare_logs { - cd $LOGS_DIR - for f in $(find . -name "*.log.2*"); do - sudo mv $f ${f/.log.*/.txt} - done - sudo gzip -9fk `find . -maxdepth 1 \! -type l -name "*.txt" | xargs ls -d` - mv *.gz $ARCHIVE_LOGS_DIR/ -} - -function check_residual_resources { - source_creds $NEW_BASE/devstack/openrc $1 $2 - gbp l3policy-list - gbp l2policy-list - gbp group-list - gbp policy-target-list - gbp policy-rule-set-list - gbp policy-rule-list - gbp policy-classifier-list - gbp policy-action-list - gbp servicechain-instance-list - gbp servicechain-node-list - gbp servicechain-spec-list - gbp service-profile-list - gbp network-service-policy-list - gbp nat-pool-list - gbp external-policy-list - gbp external-segment-list - gbp apg-list - - openstack router list - openstack network list - openstack subnet list - openstack subnet pool list - openstack port list -} diff --git a/gbpservice/tests/contrib/gate_aim_hook.sh b/gbpservice/tests/contrib/gate_aim_hook.sh deleted file mode 100644 index 64dfbc438..000000000 --- a/gbpservice/tests/contrib/gate_aim_hook.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash - -CONTRIB_DIR="$BASE/new/group-based-policy/gbpservice/tests/contrib" -cp $CONTRIB_DIR/functions-gbp . -source functions-gbp - -set -x - -trap prepare_logs ERR - -# temporary fix for bug 1693689 -export IPV4_ADDRS_SAFE_TO_USE=${DEVSTACK_GATE_IPV4_ADDRS_SAFE_TO_USE:-${DEVSTACK_GATE_FIXED_RANGE:-10.1.0.0/20}} - -prepare_gbp_aim_devstack -FORCE=yes $TOP_DIR/stack.sh - -# Use devstack functions to install mysql and psql servers -source $TOP_DIR/stackrc -source $TOP_DIR/lib/database -disable_service postgresql -enable_service mysql -initialize_database_backends -install_database diff --git a/gbpservice/tests/contrib/gate_hook.sh b/gbpservice/tests/contrib/gate_hook.sh deleted file mode 100644 index ec5b1a81d..000000000 --- a/gbpservice/tests/contrib/gate_hook.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash - -CONTRIB_DIR="$BASE/new/group-based-policy/gbpservice/tests/contrib" -cp $CONTRIB_DIR/functions-gbp . -source functions-gbp - -set -x - -trap prepare_logs ERR - -# temporary fix for bug 1693689 -export IPV4_ADDRS_SAFE_TO_USE=${DEVSTACK_GATE_IPV4_ADDRS_SAFE_TO_USE:-${DEVSTACK_GATE_FIXED_RANGE:-10.1.0.0/20}} - -prepare_gbp_devstack -FORCE=yes $TOP_DIR/stack.sh - -# Use devstack functions to install mysql and psql servers -source $TOP_DIR/stackrc -source $TOP_DIR/lib/database -disable_service postgresql -enable_service mysql -initialize_database_backends -install_database diff --git a/gbpservice/tests/contrib/gate_nfp_hook.sh b/gbpservice/tests/contrib/gate_nfp_hook.sh deleted file mode 100644 index 8f68b95d2..000000000 --- a/gbpservice/tests/contrib/gate_nfp_hook.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/bash - -# REVISIT: Temporarily disabling this job until its updated to run with Ocata -exit 1 - -CONTRIB_DIR="$BASE/new/group-based-policy/gbpservice/tests/contrib" -cp $CONTRIB_DIR/functions-gbp . -source functions-gbp - -set -x - -trap prepare_logs ERR - -# temporary fix for bug 1693689 -export IPV4_ADDRS_SAFE_TO_USE=${DEVSTACK_GATE_IPV4_ADDRS_SAFE_TO_USE:-${DEVSTACK_GATE_FIXED_RANGE:-10.1.0.0/20}} - -prepare_gbp_devstack -prepare_nfp_devstack -FORCE=yes $TOP_DIR/stack.sh - -# Use devstack functions to install mysql and psql servers -source $TOP_DIR/stackrc -source $TOP_DIR/lib/database -disable_service postgresql -enable_service mysql -initialize_database_backends -install_database - -# Set up the 'openstack_citest' user and database in each backend -tmp_dir=`mktemp -d` - -cat << EOF > $tmp_dir/mysql.sql -CREATE DATABASE openstack_citest; -CREATE USER 'openstack_citest'@'localhost' IDENTIFIED BY 'openstack_citest'; -CREATE USER 'openstack_citest' IDENTIFIED BY 'openstack_citest'; -GRANT ALL PRIVILEGES ON *.* TO 'openstack_citest'@'localhost'; -GRANT ALL PRIVILEGES ON *.* TO 'openstack_citest'; -FLUSH PRIVILEGES; -EOF -/usr/bin/mysql -u root < $tmp_dir/mysql.sql diff --git a/gbpservice/tests/contrib/gbpfunctests/README.md b/gbpservice/tests/contrib/gbpfunctests/README.md deleted file mode 100755 index 3a056915e..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/README.md +++ /dev/null @@ -1,20 +0,0 @@ -# gbpfunctests: Integration and functional tests for OpenStack GBP - -Instructions: - -1. Make sure the library imports successfully: -export PYTHONPATH="${PYTHONPATH}:" - -2. Navigate to the testcases directory: -cd /testcases - -3. Run the full suite by executing: -python suite_run.py" - -4. Each GBP resource can tested by running: -python tc_gbp_ - -or to test all resources: - -python tc_gbp_* - diff --git a/gbpservice/tests/contrib/gbpfunctests/libs/__init__.py b/gbpservice/tests/contrib/gbpfunctests/libs/__init__.py deleted file mode 100755 index e69de29bb..000000000 diff --git a/gbpservice/tests/contrib/gbpfunctests/libs/config_libs.py b/gbpservice/tests/contrib/gbpfunctests/libs/config_libs.py deleted file mode 100755 index 08a2fdcb0..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/libs/config_libs.py +++ /dev/null @@ -1,403 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import os -import re -import string -import subprocess -import sys - -import six - - -# Initialize logging -logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) -_log = logging.getLogger() -_log.setLevel(logging.INFO) - - -class Gbp_Config(object): - - def __init__(self): - self.err_strings = ['Unable', 'Conflict', 'Bad Request', - 'Error', 'Unknown', 'Exception', 'Invalid', - 'read-only', 'not supported', - 'prefix greater than subnet mask', - 'reference the non shared resource', - 'cannot be shared by driver'] - - def keystone_creds(self): - creds = {} - creds['username'] = os.environ['OS_USERNAME'] - creds['password'] = os.environ['OS_PASSWORD'] - creds['auth_url'] = os.environ['OS_AUTH_URL'] - creds['tenant_name'] = os.environ['OS_TENANT_NAME'] - return creds - - def get_uuid(self, cmd_out): - match = re.search("\\bid\\b\s+\| (.*) \|", cmd_out, re.I) - if match is not None: - obj_uuid = match.group(1) - return obj_uuid.rstrip() - else: - return 0 - - def gbp_action_config(self, cmd_val, name_uuid, **kwargs): - """ - -- cmd_val== 0:delete; 1:create; 2:update - -- name_uuid == UUID or name_string - Create/Update/Delete Policy Action - Returns assigned UUID on Create - kwargs addresses the need for passing required/optional params - """ - if cmd_val == '' or name_uuid == '': - _log.info('''Function Usage: gbp_action_config 0 "abc"\n - --cmd_val == 0:delete; 1:create; 2:update\n - -- name_uuid == UUID or name_string\n''') - return 0 - # Build the command with mandatory param 'name_uuid' - if cmd_val == 0: - cmd = 'gbp policy-action-delete ' + str(name_uuid) - if cmd_val == 1: - cmd = 'gbp policy-action-create ' + str(name_uuid) - if cmd_val == 2: - cmd = 'gbp policy-action-update ' + str(name_uuid) - # Build the cmd string for optional/non-default args/values - for arg, value in list(kwargs.items()): - cmd = cmd + " --" + ("%s %s" % (arg, value)) - _log.info(cmd) - # Execute the policy-action-config-cmd - cmd_out = subprocess.getoutput(cmd) - _log.info(cmd_out) - # Catch for non-exception error strings, even though try clause - # succeeded - if self.cmd_error_check(cmd_out) == 0: - return 0 - # If "create" cmd succeeded then parse the cmd_out to extract the UUID - if cmd_val == 1: - action_uuid = self.get_uuid(cmd_out) - return action_uuid - - def gbp_classif_config(self, cmd_val, classifier_name, **kwargs): - """ - -- cmd_val== 0:delete; 1:create; 2:update - -- classifier_name == UUID or name_string - Create/Update/Delete Policy Classifier - Returns assigned UUID on Create - kwargs addresses the need for passing required/optional params - """ - if cmd_val == '' or classifier_name == '': - _log.info('''Function Usage: gbp_classifier_config 0 "abc"\n - --cmd_val == 0:delete; 1:create; 2:update\n - -- classifier_name == UUID or name_string\n''') - return 0 - # Build the command with mandatory param 'classifier_name' - if cmd_val == 0: - cmd = 'gbp policy-classifier-delete ' + str(classifier_name) - if cmd_val == 1: - cmd = 'gbp policy-classifier-create ' + str(classifier_name) - if cmd_val == 2: - cmd = 'gbp policy-classifier-update ' + str(classifier_name) - # Build the cmd string for optional/non-default args/values - for arg, value in list(kwargs.items()): - cmd = cmd + " --" + "%s %s" % (arg, value) - - # Execute the policy-classifier-config-cmd - cmd_out = subprocess.getoutput(cmd) - # Catch for non-exception error strings, even though try clause - # succeeded - if self.cmd_error_check(cmd_out) == 0: - return 0 - # If try clause succeeds for "create" cmd then parse the cmd_out - # to extract the UUID - if cmd_val == 1: - classifier_uuid = self.get_uuid(cmd_out) - return classifier_uuid - - def gbp_policy_cfg_all(self, cmd_val, cfgobj, name_uuid, **kwargs): - """ - --cfgobj== policy-*(where *=action;classifer,rule,ruleset,targetgroup, - target - --cmd_val== 0:delete; 1:create; 2:update - --name_uuid == UUID or name_string - Create/Update/Delete Policy Object - Returns assigned UUID on Create - kwargs addresses the need for passing required/optional params - """ - cfgobj_dict = { - "action": "policy-action", - "classifier": "policy-classifier", - "rule": "policy-rule", - "ruleset": "policy-rule-set", - "group": "policy-target-group", - "target": "policy-target", - "l2p": "l2policy", - "l3p": "l3policy", - "nsp": "network-service-policy", - "extseg": "external-segment", - "extpol": "external-policy", - "natpool": "nat-pool"} - - if cfgobj != '': - if cfgobj not in cfgobj_dict: - raise KeyError - if cmd_val == '' or name_uuid == '': - _log.info('''Function Usage: gbp_policy_cfg_all 'rule' 0 "abc"\n - --cmd_val == 0:delete; 1:create; 2:update\n - -- name_uuid == UUID or name_string\n''') - return 0 - - # Build the command with mandatory params - if cmd_val == 0: - cmd = 'gbp %s-delete ' % cfgobj_dict[cfgobj] + str(name_uuid) - if cmd_val == 1: - cmd = 'gbp %s-create ' % cfgobj_dict[cfgobj] + str(name_uuid) - if cmd_val == 2: - cmd = 'gbp %s-update ' % cfgobj_dict[cfgobj] + str(name_uuid) - # Build the cmd string for optional/non-default args/values - for arg, value in list(kwargs.items()): - if arg.startswith('_'): - # Parameter not supported by CLI, leave it as is - arg = arg[1:] - cmd = cmd + " --" + "%s %s" % (arg, value) - else: - arg = string.replace(arg, '_', '-') - cmd = cmd + " --" + "%s=%s" % (arg, value) - _log.info(cmd) - # Execute the cmd - cmd_out = subprocess.getoutput(cmd) - # Catch for non-exception error strings, even though try clause - # succeeded - if self.cmd_error_check(cmd_out) == 0: - return 0 - # If try clause succeeds for "create" cmd then parse the cmd_out to - # extract the UUID of the object - - try: - if cmd_val == 1 and cfgobj == "group": - obj_uuid = self.get_uuid(cmd_out) - match = re.search("\\bl2_policy_id\\b\s+\| (.*) \|", - cmd_out, re.I) - l2pid = match.group(1) - match = re.search("\\bsubnets\\b\s+\| (.*) \|", - cmd_out, re.I) - subnetid = match.group(1) - return obj_uuid, l2pid.rstrip(), subnetid.rstrip() - if cmd_val == 1 and cfgobj == "target": - obj_uuid = self.get_uuid(cmd_out) - match = re.search("\\bport_id\\b\s+\| (.*) \|", - cmd_out, re.I) - neutr_port_id = match.group(1) - return obj_uuid.rstrip(), neutr_port_id.rstrip() - if cmd_val == 1 and cfgobj == "l2p": - obj_uuid = self.get_uuid(cmd_out) - match = re.search("\\l3_policy_id\\b\s+\| (.*) \|", - cmd_out, re.I) - l3p_uuid = match.group(1) - return obj_uuid.rstrip(), l3p_uuid.rstrip() - if cmd_val == 1: - obj_uuid = self.get_uuid(cmd_out) - return obj_uuid.rstrip() - except Exception: - exc_type, exc_value, exc_traceback = sys.exc_info() - _log.info('Exception Type = %s, Exception Object = %s' % ( - exc_type, exc_value)) - return 0 - return 1 - - def gbp_policy_cfg_upd_all(self, cfgobj, name_uuid, attr): - """ - --cfgobj== policy-*(where *=action;classifer,rule,ruleset,targetgroup, - target - --name_uuid == UUID or name_string - --attr == MUST be a dict, where key: attribute_name, while val: - attribute's value(new value to update) - Updates Policy Objects' editable attributes - """ - cfgobj_dict = {"action": "policy-action", - "classifier": "policy-classifier", - "rule": "policy-rule", - "ruleset": "policy-rule-set", - "group": "policy-target-group", - "target": "policy-target", - "l2p": "l2policy", - "l3p": "l3policy", - "nsp": "network-service-policy", - "extseg": "external-segment", - "extpol": "external-policy", - "natpool": "nat-pool"} - if cfgobj != '': - if cfgobj not in cfgobj_dict: - raise KeyError - if name_uuid == '' or not isinstance(attr, dict): - _log.info('''Function Usage: gbp_policy_cfg_upd_all 'rule' "abc" - {attr:attr_val}\n - --cmd_val == 0:delete; 1:create; 2:update\n - -- name_uuid == UUID or name_string\n''') - return 0 - - # Build the command with mandatory params - cmd = 'gbp %s-update ' % cfgobj_dict[cfgobj] + str(name_uuid) - # Build the cmd string for optional/non-default args/values - for arg, value in six.iteritems(attr): - if '_' in arg: - arg = string.replace(arg, '_', '-') - cmd = cmd + " --" + ("%s %s" % (arg, value)) - _log.info(cmd) - # Execute the update cmd - cmd_out = subprocess.getoutput(cmd) - # Catch for non-exception error strings, even though try clause - # succeeded - if self.cmd_error_check(cmd_out) == 0: - return 0 - return 1 - - def gbp_del_all_anyobj(self, cfgobj): - """ - This function deletes all entries for any policy-object - """ - cfgobj_dict = {"action": "policy-action", - "classifier": "policy-classifier", - "rule": "policy-rule", - "ruleset": "policy-rule-set", - "group": "group", - "target": "policy-target", - "l2p": "l2policy", - "l3p": "l3policy", - "nsp": "network-service-policy", - "node": "servicechain-node", - "spec": "servicechain-spec", - "extseg": "external-segment", - "extpol": "external-policy", - "natpool": "nat-pool"} - if cfgobj != '': - if cfgobj not in cfgobj_dict: - raise KeyError - # Build the command with mandatory params - cmd = 'gbp %s-list -c id ' % cfgobj_dict[cfgobj] - cmd_out = subprocess.getoutput(cmd) - _out = cmd_out.split('\n') - final_out = _out[3:len(_out) - 1] - _log.info("\nThe Policy Object %s to be deleted = \n%s" % ( - cfgobj_dict[cfgobj], cmd_out)) - for item in final_out: - item = item.strip(' |') - cmd = 'gbp %s-delete ' % cfgobj_dict[cfgobj] + str(item) - cmd_out = subprocess.getoutput(cmd) - _log.info(cmd_out) - return 1 - - def gbp_sc_cfg_all(self, cmd_val, cfgobj, name_uuid, nodes="", - svc_type='lb'): - """ - ::cmd_val= 0: delete; 1:create - ::cfgobj = servicechain-*(where *=node;spec) - ::name_uuid = UUID or name_string - ::svc_type = LOADBALANCER or FIREWALL, defaulted to LB - Create/Update/Delete Policy Object - Returns assigned UUID on Create - kwargs addresses the need for passing required/optional params - """ - cfgobj_dict = {"node": "servicechain-node", - "spec": "servicechain-spec"} - if cfgobj != '': - if cfgobj not in cfgobj_dict: - raise KeyError - if cmd_val == '' or name_uuid == '': - _log.info( - '''Function Usage: gbp_sc_cfg_all(0,"node", - "name or uuid")\n''') - return 0 - - # Build the command with mandatory params - if cmd_val == 0: - cmd = 'gbp %s-delete ' % cfgobj_dict[cfgobj] + str(name_uuid) - if cmd_val == 1 and cfgobj == 'spec': - cmd = ('gbp %s-create ' % cfgobj_dict[cfgobj] + str(name_uuid) + - ' --nodes "%s"' % nodes) - if cmd_val == 1 and cfgobj == 'node': - if svc_type == 'lb': - service = 'LOADBALANCER' - else: - service = 'FIREWALL' - cmd = ('gbp %s-create ' % cfgobj_dict[cfgobj] + str(name_uuid) + - ' --template-file %s.template' % svc_type + - ' --servicetype ' + service) - _log.info(cmd) - # Execute the policy-rule-config-cmd - cmd_out = subprocess.getoutput(cmd) - - # Catch for non-exception error strings, even though try clause - # succeeded - if self.cmd_error_check(cmd_out) == 0: - return 0 - if cmd_val == 1: - obj_uuid = self.get_uuid(cmd_out) - return obj_uuid - - def neutron_cfg(self, cmd_val, cfg_obj, name_uuid, **kwargs): - """ - --cfgobj== net,subnet - --cmd_val== 0:delete; 1:create; 2:update - --name_uuid == UUID or name_string - Create/Update/Delete Policy Object - Returns assigned UUID on Create - kwargs addresses the need for passing required/optional params - """ - cfgobj_dict = {"network": "net", "subnet": "subnet"} - if cfg_obj != '': - if cfg_obj not in cfgobj_dict: - raise KeyError - if cmd_val == '' or name_uuid == '': - _log.info('''Function Usage: gbp_policy_cfg_all 'rule' 0 "abc"\n - --cmd_val == 0:delete; 1:create; 2:update\n - -- name_uuid == UUID or name_string\n''') - return 0 - - # Build the command with mandatory params - if cmd_val == 0: - cmd = 'neutron %s-delete ' % cfgobj_dict[cfg_obj] + str(name_uuid) - if cmd_val == 1: - cmd = 'neutron %s-create ' % cfgobj_dict[cfg_obj] + str(name_uuid) - if cmd_val == 2: - cmd = 'neutron %s-update ' % cfgobj_dict[cfg_obj] + str(name_uuid) - # Build the cmd string for optional/non-default args/values - for arg, value in list(kwargs.items()): - if '_' in arg: - arg = string.replace(arg, '_', '-') - cmd = cmd + " --" + "".join('%s=%s' % (arg, value)) - _log.info(cmd) - # Execute the cmd - cmd_out = subprocess.getoutput(cmd) - - # Catch for non-exception error strings, even though try clause - # succeeded - if self.cmd_error_check(cmd_out) == 0: - return 0 - if cmd_val == 1: - obj_uuid = self.get_uuid(cmd_out) - return obj_uuid - - def cmd_error_check(self, cmd_out): - """ - Verifies whether executed cmd has any known error string - """ - for err in self.err_strings: - if re.search('\\b%s\\b' % (err), cmd_out, re.I): - _log.info(cmd_out) - _log.info( - "Cmd execution failed! with this Return Error: \n%s" % ( - cmd_out)) - return 0 diff --git a/gbpservice/tests/contrib/gbpfunctests/libs/utils_libs.py b/gbpservice/tests/contrib/gbpfunctests/libs/utils_libs.py deleted file mode 100755 index 7df51337a..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/libs/utils_libs.py +++ /dev/null @@ -1,108 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import itertools -import re -import subprocess -import sys - -import paramiko -import prettytable -import six - - -def sshconnect(hostname, user, passwd): - sshclient = paramiko.SSHClient() - sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - try: - sshclient.connect(hostname, username=user, password=passwd) - except Exception: - # raise ErrorConnectingToServer( - # "Error connecting to server %s: %s" % - # (hostname, e)) - # sshclient = None - raise - return sshclient - - -def report_table(suite_name): - ps = subprocess.Popen(['grep', - '-r', - 'TESTCASE', - '/tmp/%s.log' % (suite_name)], - stdout=subprocess.PIPE) - output = ps.communicate()[0] - output = output.splitlines() - line = 0 - tc_dict = {} - while line < len(output): - find1 = re.search('\\b(TESTCASE_GBP_.*)\\b: (.*)', output[line], re.I) - if find1 is not None: - line += 1 - if line <= len(output) - 1: - find2 = re.search( - '\\b%s\\b: (.*)' % - (find1.group(1)), output[line], re.I) - if find2 is not None: - tc_dict[find1.group(1)] = find2.group(1), find1.group(2) - line += 1 - table = prettytable.PrettyTable(["TESTCASE_ID", "RESULTS", - "TESTCASE_HEADER"]) - table.padding_width = 1 - for key, val in six.iteritems(tc_dict): - table.add_row(["%s" % (key), "%s" % (val[0]), "%s" % (val[1])]) - return table - - -def report_results(suite_name, txt_file): - orig_stdout = sys.stdout - f = open('%s' % (txt_file), 'a') - sys.stdout = f - report = report_table(suite_name) - print(report) - sys.stdout = orig_stdout - f.close() - - -def gen_tc_header(): - comb_list = [ - ['same_leaf', 'two_leafs'], [ - 'same_host', 'two_hosts'], [ - 'same_ptg', 'two_ptgs'], [ - 'same_L3_subnet', 'two_L3_subnets'], [ - 'same_L2_bd', 'two_L2_bds']] - - out_hdr_list = list(itertools.product(*comb_list)) - out_headers = [] - for hdr in out_hdr_list: - header = 'test_' + '_'.join(str(i) for i in hdr) - out_headers.append(header) - - proto = list(itertools.combinations( - ['icmp', 'tcp', 'udp', 'dhcp', 'arp'], 2)) - proto_hdrs = [] - for hdr in proto: - proto_header = '_'.join(str(i) for i in hdr) - proto_hdrs.append(proto_header) - in_hdrs = list(itertools.product(out_headers, proto_hdrs)) - final_headers = [] - for hdr in in_hdrs: - tc_header = '_'.join(str(i) for i in hdr) - final_headers.append(tc_header) - table = prettytable.PrettyTable(["TESTCASE_ID", "STATUS", - "TESTCASE_HEADER"]) - table.padding_width = 1 - for i in range(len(final_headers)): - table.add_row(["TESTCASE_DP_%s" % - (i + 1), "TBA", "%s" % - (final_headers[i])]) - print(table) diff --git a/gbpservice/tests/contrib/gbpfunctests/libs/verify_libs.py b/gbpservice/tests/contrib/gbpfunctests/libs/verify_libs.py deleted file mode 100755 index 94599bfde..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/libs/verify_libs.py +++ /dev/null @@ -1,458 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import re -import subprocess - -import yaml - -# Initialize logging -logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) -_log = logging.getLogger() -_log.setLevel(logging.INFO) - - -orig_getoutput = subprocess.getoutput - - -def getoutput(cmd): - _log.info('Running cmd: %s\n' % (cmd)) - cmd_out = orig_getoutput(cmd) - _log.info('Cmd output: %s\n' % (cmd_out)) - return cmd_out - - -subprocess.getoutput = getoutput - - -class Gbp_Verify(object): - - def __init__(self): - """ - Init def - """ - self.err_strings = [ - 'Conflict', - 'Bad Request', - 'Error', - 'Unknown', - 'Unable'] - - def gbp_action_verify(self, cmd_val, action_name, *args, **kwargs): - """ - -- cmd_val== 0:list; 1:show - -- action_name == UUID or name_string - List/Show Policy Action - kwargs addresses the need for passing required/optional params - """ - if cmd_val == '' or action_name == '': - _log.info('''Function Usage: gbp_action_verify 0 "abc" \n - --cmd_val == 0:list; 1:show\n - -- action_name == UUID or name_string\n''') - return 0 - # Build the command with mandatory param 'action_name' - if cmd_val == 0: - cmd = 'gbp policy-action-list | grep %s' % str(action_name) - for arg in args: - cmd = cmd + ' | grep %s' % arg - if cmd_val == 1: - cmd = "gbp policy-action-show " + str(action_name) - - # Execute the policy-action-verify-cmd - cmd_out = subprocess.getoutput(cmd) - - # Catch for non-exception error strings, even though try clause - # succeeded - for err in self.err_strings: - if re.search('\\b%s\\b' % (err), cmd_out, re.I): - _log.info(cmd_out) - _log.info( - "Cmd execution failed! with this Return Error: \n%s" % - (cmd_out)) - return 0 - if cmd_val == 0: - for arg in args: - if cmd_out.find(arg) == -1 or cmd_out.find(action_name) == -1: - _log.info(cmd_out) - _log.info( - "The Attribute== %s DID NOT MATCH for the Action == %s" - "in LIST cmd" % (arg, action_name)) - return 0 - # If try clause succeeds for "verify" cmd then parse the cmd_out to - # match the user-fed expected attributes & their values - if cmd_val == 1: - for arg, val in list(kwargs.items()): - if re.search("\\b%s\\b\s+\| \\b%s\\b.*" % - (arg, val), cmd_out, re.I) is None: - _log.info(cmd_out) - _log.info( - "The Attribute== %s and its Value== %s DID NOT MATCH" - "for the Action == %s" % - (arg, val, action_name)) - return 0 - - return 1 - - def gbp_classif_verify(self, cmd_val, classifier_name, *args, **kwargs): - """ - -- cmd_val== 0:list; 1:show - -- classifier_name == UUID or name_string - List/Show Policy Action - kwargs addresses the need for passing required/optional params - """ - if cmd_val == '' or classifier_name == '': - _log.info('''Function Usage: gbp_classif_verify(0,name) \n - --cmd_val == 0:list 1:show\n - -- classifier_name == UUID or name_string\n''') - return 0 - # Build the command with mandatory param 'classifier_name' - if cmd_val == 0: - cmd = 'gbp policy-classifier-list | grep %s' % str(classifier_name) - for arg in args: - cmd = cmd + ' | grep %s' % arg - if cmd_val == 1: - cmd = "gbp policy-classifier-show " + str(classifier_name) - # Execute the policy-classifier-verify-cmd - cmd_out = subprocess.getoutput(cmd) - - # Catch for non-exception error strings, even though try clause - # succeeded - for err in self.err_strings: - if re.search('\\b%s\\b' % (err), cmd_out, re.I): - _log.info(cmd_out) - _log.info( - "Cmd execution failed! with this Return Error: \n%s" % - (cmd_out)) - return 0 - if cmd_val == 0: - for arg in args: - if cmd_out.find(arg) == - \ - 1 or cmd_out.find(classifier_name) == -1: - _log.info(cmd_out) - _log.info( - "The Attribute== %s DID NOT MATCH for the Classifier " - "== %s in LIST cmd" % - (arg, classifier_name)) - return 0 - # If try clause succeeds for "verify" cmd then parse the cmd_out to - # match the user-fed expected attributes & their values - if cmd_val == 1: - for arg, val in list(kwargs.items()): - if re.search("\\b%s\\b\s+\| \\b%s\\b.*" % - (arg, val), cmd_out, re.I) is None: - _log.info(cmd_out) - _log.info( - "The Attribute== %s and its Value== %s DID NOT MATCH " - "for the Claasifier == %s" % - (arg, val, classifier_name)) - return 0 - - return 1 - - def gbp_policy_verify_all(self, cmd_val, verifyobj, - name_uuid, *args, **kwargs): - """ - --verifyobj== policy-*(where *=action;classifer,rule,rule-set, - target-group,target) - --cmd_val== 0:list; 1:show - kwargs addresses the need for passing required/optional params - """ - verifyobj_dict = { - "action": "policy-action", - "classifier": "policy-classifier", - "rule": "policy-rule", - "ruleset": "policy-rule-set", - "group": "group", - "target": "policy-target"} - if verifyobj != '': - if verifyobj not in verifyobj_dict: - raise KeyError - if cmd_val == '' or name_uuid == '': - _log.info('''Function Usage: gbp_policy_verify_all(0,'action', - 'name_uuid')\n - --cmd_val == 0:list; 1:show\n - -- name_uuid == UUID or name_string\n''') - return 0 - # Build the command with mandatory params - if cmd_val == 0: - cmd = 'gbp %s-list | grep ' % verifyobj_dict[ - verifyobj] + str(name_uuid) - for arg in args: - cmd = cmd + ' | grep %s' % arg - if cmd_val == 1: - cmd = 'gbp %s-show ' % verifyobj_dict[verifyobj] + str(name_uuid) - # Execute the policy-object-verify-cmd - cmd_out = subprocess.getoutput(cmd) - # Catch for non-exception error strings - for err in self.err_strings: - if re.search('\\b%s\\b' % (err), cmd_out, re.I): - _log.info(cmd_out) - _log.info( - "Cmd execution failed! with this Return Error: \n%s" % - (cmd_out)) - return 0 - if cmd_val == 0: - if name_uuid not in cmd_out: # uuid not found - return 0 - else: - for arg in args: - if (cmd_out.find(arg) == -1 or - cmd_out.find(name_uuid) == -1): - _log.info(cmd_out) - _log.info( - "The Attribute== %s DID NOT MATCH for the " - "Policy Object == %s in LIST cmd" % - (arg, verifyobj)) - return 0 - # If "verify" cmd succeeds then parse the cmd_out to match the user-fed - # expected attributes & their values - if cmd_val == 1: - for arg, val in list(kwargs.items()): - if re.search("\\b%s\\b\s+\| \\b%s\\b.*" % - (arg, val), cmd_out, re.I) is None: - _log.info(cmd_out) - _log.info( - "The Attribute== %s and its Value== %s DID NOT MATCH " - "for the PolicyObject == %s" % - (arg, val, verifyobj)) - return 0 - - return 1 - - def gbp_l2l3ntk_pol_ver_all( - self, cmd_val, verifyobj, name_uuid, ret='', *args, **kwargs): - """ - --verifyobj== *policy(where *=l2;l3,network) - --cmd_val== 0:list; 1:show - --ret=='default' <<< function will return some attribute values - depending upon the verifyobj - kwargs addresses the need for passing required/optional params - """ - verifyobj_dict = { - "l2p": "l2policy", - "l3p": "l3policy", - "nsp": "network-service-policy"} - if verifyobj != '': - if verifyobj not in verifyobj_dict: - raise KeyError - if cmd_val == '' or name_uuid == '': - _log.info('''Function Usage: gbp_l2l3ntk_pol_ver_all(0, - 'l2p','name') \n - --cmd_val == 0:list; 1:show\n - --name_uuid == UUID or name_string\n''') - return 0 - # Build the command with mandatory params - if cmd_val == 0: - cmd = 'gbp %s-list | grep ' % verifyobj_dict[ - verifyobj] + str(name_uuid) - for arg in args: - cmd += ' | grep %s' % arg - if cmd_val == 1: - cmd = 'gbp %s-show ' % verifyobj_dict[verifyobj] + str(name_uuid) - # Execute the policy-object-verify-cmd - cmd_out = subprocess.getoutput(cmd) - # _log.info(cmd_out) - # Catch for non-exception error strings - for err in self.err_strings: - if re.search('\\b%s\\b' % (err), cmd_out, re.I): - _log.info(cmd_out) - _log.info( - "Cmd execution failed! with this Return Error: \n%s" % - (cmd_out)) - return 0 - if cmd_val == 0: - if name_uuid not in cmd_out: # uuid not found - return 0 - else: - for arg in args: - if (cmd_out.find(arg) == -1 or - cmd_out.find(name_uuid) == -1): - _log.info(cmd_out) - _log.info( - "The Attribute== %s DID NOT MATCH for the Policy " - "Object == %s in LIST cmd" % - (arg, verifyobj)) - return 0 - # If "verify" succeeds cmd then parse the cmd_out to match the user-fed - # expected attributes & their values - if cmd_val == 1 and ret == 'default': - for arg, val in list(kwargs.items()): - if re.search("\\b%s\\b\s+\| \\b%s\\b.*" % - (arg, val), cmd_out, re.I) is None: - # incase of attribute has more than one value then - # then below function will help us validating the values - # or the only value among all for the given attr. - # Example: L2P can have multiple PTGs, L3P can have multi - # L2Ps - if not self.gbp_obj_ver_attr_all_values( - verifyobj, name_uuid, arg, [val]): - _log.info( - "The Attribute== %s and its Value== %s " - "DID NOT MATCH " - "for the PolicyObject == %s" % ( - arg, val, verifyobj)) - return 0 - if verifyobj == "l2p": - match = re.search( - "\\bl3_policy_id\\b\s+\| (.*) \|", cmd_out, re.I) - l3pid = match.group(1) - match = re.search( - "\\bnetwork_id\\b\s+\| (.*) \|", cmd_out, re.I) - ntkid = match.group(1) - return l3pid.rstrip(), ntkid.rstrip() - if verifyobj == "l3p": - match = re.search("\\brouters\\b\s+\| (.*) \|", cmd_out, re.I) - rtrid = match.group(1) - return rtrid.rstrip() - elif cmd_val == 1: - for arg, val in list(kwargs.items()): - if arg == 'network_service_params': - if re.findall('(%s)' % (val), cmd_out) == []: - _log.info(cmd_out) - _log.info( - "The Attribute== %s and its Value== %s DID NOT " - "MATCH for the PolicyObject == %s" % - (arg, val, verifyobj)) - return 0 - elif re.search("\\b%s\\b\s+\| \\b%s\\b.*" % (arg, val), - cmd_out, re.I) is None: - _log.info(cmd_out) - _log.info( - "The Attribute== %s and its Value== %s DID NOT MATCH " - "for the PolicyObject == %s" % - (arg, val, verifyobj)) - return 0 - else: - return 1 - - def neut_ver_all(self, verifyobj, name_uuid, ret='', **kwargs): - """ - --verifyobj== net,subnet,port,router - --ret=='default' <<< function will return some attribute values - depending upon the verifyobj - kwargs addresses the need for passing required/optional params - """ - if name_uuid == '': - _log.info('''Function Usage: neut_ver_all('net','name')\n - -- name_uuid == UUID or name_string\n''') - return 0 - - # Build the command with mandatory params - cmd = 'neutron %s-show ' % verifyobj + str(name_uuid) - _log.info('Neutron Cmd == %s\n' % (cmd)) - # Execute the policy-object-verify-cmd - cmd_out = subprocess.getoutput(cmd) - _log.info(cmd_out) - # Catch for non-exception error strings - for err in self.err_strings: - if re.search('\\b%s\\b' % (err), cmd_out, re.I): - _log.info(cmd_out) - _log.info( - "Neutron Cmd execution failed! with this Return Error: " - "\n%s" % cmd_out) - return 0 - if ret != '': - match = re.search("\\b%s\\b\s+\| (.*) \|" % (ret), cmd_out, re.I) - if match is not None: - return match.group(1).rstrip() - else: - return 0 - for arg, val in list(kwargs.items()): - if isinstance(val, list): # More than 1 value is to be verified - for i in val: - if cmd_out.find(i) == -1: - _log.info(cmd_out) - _log.info( - "The Attribute== %s and its Value== %s DID NOT " - "MATCH for the NeutronObject == %s" % - (arg, i, verifyobj)) - return 0 - else: - if re.search("\\b%s\\b\s+\| \\b%s\\b.*" % - (arg, val), cmd_out, re.I) is None: - _log.info(cmd_out) - _log.info( - "The Attribute== %s and its Value== %s DID NOT MATCH " - "for the NeutronObject == %s" % - (arg, val, verifyobj)) - return 0 - return 1 - - def gbp_obj_ver_attr_all_values(self, verifyobj, name_uuid, attr, values): - """ - Function will verify multiple entries for any given attribute - of a Policy Object - --values=Must be a list - """ - verifyobj_dict = { - "action": "policy-action", - "classifier": "policy-classifier", - "rule": "policy-rule", - "ruleset": "policy-rule-set", - "group": "group", - "target": "policy-target", - "l2p": "l2policy", - "l3p": "l3policy", - "nsp": "network-service-policy"} - if verifyobj != '': - if verifyobj not in verifyobj_dict: - raise KeyError - if not isinstance(values, list): - raise TypeError - # Build the command with mandatory params - cmd = ('gbp %s-show ' % verifyobj_dict[verifyobj] + - str(name_uuid) + ' -F %s' % (attr)) - # Execute the policy-object-verify-cmd - cmd_out = subprocess.getoutput(cmd) - # Catch for non-exception error strings - for err in self.err_strings: - if re.search('\\b%s\\b' % (err), cmd_out, re.I): - _log.info( - "Cmd execution failed! with this Return Error: \n%s" % - (cmd_out)) - return 0 - _misses = [] - for val in values: - if cmd_out.find(val) == -1: - _misses.append(val) - if len(_misses) > 0: - _log.info( - "\nFollowing Values of the Attribute for the Policy Object " - "was NOT FOUND=%s" % - (_misses)) - return 0 - return 1 - - def get_uuid_from_stack(self, yaml_file, heat_stack_name): - """ - Fetches the UUID of the GBP Objects created by Heat - """ - with open(yaml_file, 'rt') as f: - heat_conf = yaml.load(f) - obj_uuid = {} - # This comprise dict with keys as in [outputs] block of yaml-based - # heat template - outputs_dict = heat_conf["outputs"] - print(outputs_dict) - for key in list(outputs_dict.keys()): - cmd = 'heat stack-show %s | grep -B 2 %s' % (heat_stack_name, key) - print(cmd) - cmd_out = subprocess.getoutput(cmd) - print(cmd_out) - match = re.search('\"\\boutput_value\\b\": \"(.*)\"', - cmd_out, re.I) - if match is not None: - obj_uuid[key] = match.group(1) - return obj_uuid diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/__init__.py b/gbpservice/tests/contrib/gbpfunctests/testcases/__init__.py deleted file mode 100755 index e69de29bb..000000000 diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/func_neg.txt b/gbpservice/tests/contrib/gbpfunctests/testcases/func_neg.txt deleted file mode 100755 index e69de29bb..000000000 diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/fw.template b/gbpservice/tests/contrib/gbpfunctests/testcases/fw.template deleted file mode 100755 index 6d70394a8..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/fw.template +++ /dev/null @@ -1,36 +0,0 @@ -{ - "heat_template_version": "2013-05-23", - "resources": { - "Firewall_service": { - "type": "OS::Neutron::Firewall", - "properties": { - "admin_state_up": true, - "firewall_policy_id": {"get_resource": "Firewall_policy"}, - "name": "testFirewall", - "description": "test Firewall" - } - }, - "Firewall_policy": { - "type": "OS::Neutron::FirewallPolicy", - "properties": { - "shared": false, - "description": "test firewall policy", - "name": "testFWPolicy", - "firewall_rules": [{"get_resource": "Firewall_rule"}], - "audited": true - } - }, - "Firewall_rule": { - "type": "OS::Neutron::FirewallRule", - "properties": { - "protocol": "tcp", - "description": "firewall rule 1", - "enabled": true, - "destination_port": "80", - "shared": false, - "action": "allow", - "name": "testFw" - } - } - } -} diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/lb.template b/gbpservice/tests/contrib/gbpfunctests/testcases/lb.template deleted file mode 100755 index a02cb8b1d..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/lb.template +++ /dev/null @@ -1,73 +0,0 @@ -{ - "AWSTemplateFormatVersion" : "2010-09-09", - "Description": "Template to test Haproxy Loadbalacer service", - - "Parameters": { - "Subnet": { - "Description": "Pool Subnet CIDR, on which VIP port should be created", - "Type": "String" - }, - "PoolMemberIPs": { - "Description": "Pool Member IP Address", - "Type": "String" - }, - "vip_ip": { - "Description": "VIP IP Address", - "Type": "String" - } - }, - - "Resources" : { - "HttpHM": { - "Type": "OS::Neutron::HealthMonitor", - "Properties": { - "admin_state_up": true, - "delay": 20, - "expected_codes": "200", - "http_method": "GET", - "max_retries": 3, - "timeout": 10, - "type": "HTTP", - "url_path": "/" - } - }, - "HaproxyPool": { - "Type": "OS::Neutron::Pool", - "Properties": { - "admin_state_up": true, - "description": "Haproxy pool from teplate", - "lb_method": "ROUND_ROBIN", - "monitors": [{"Ref":"HttpHM"}], - "name": "Haproxy pool", - "protocol": "HTTP", - "subnet_id": {"Ref":"Subnet"}, - "vip": { - "subnet": {"Ref":"Subnet"}, - "address": {"Ref":"vip_ip"}, - "name": "Haproxy vip", - "protocol_port": 80, - "connection_limit": -1, - "admin_state_up": true, - "description": "Haproxy vip from template" - } - } - }, - "HaproxyLb": { - "Type": "OS::Neutron::LoadBalancer", - "Properties": { - "pool_id": {"Ref":"HaproxyPool"}, - "protocol_port": 80 - } - }, - "Member1": { - "Type": "OS::Neutron::PoolMember", - "Properties": { - "address": {"Ref":"PoolMemberIPs"}, - "admin_state_up": true, - "pool_id": {"Ref":"HaproxyPool"}, - "protocol_port": 80, - "weight": 1 - } - } - } -} diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/lb_multiple_members.template b/gbpservice/tests/contrib/gbpfunctests/testcases/lb_multiple_members.template deleted file mode 100755 index 71f12aa75..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/lb_multiple_members.template +++ /dev/null @@ -1,101 +0,0 @@ -{ - "AWSTemplateFormatVersion" : "2010-09-09", - "Description": "Template to test Haproxy Loadbalacer service", - - "Parameters": { - "Subnet": { - "Description": "Pool Subnet CIDR, on which VIP port should be created", - "Type": "String" - }, - "PoolMemberIP1": { - "Description": "Pool Member IP Address", - "Type": "String" - }, - "PoolMemberIP2": { - "Description": "Pool Member IP Address", - "Type": "String" - }, - "PoolMemberIP3": { - "Description": "Pool Member IP Address", - "Type": "String" - }, - "vip_ip": { - "Description": "VIP IP Address", - "Type": "String" - } - }, - - "Resources" : { - "HttpHM": { - "Type": "OS::Neutron::HealthMonitor", - "Properties": { - "admin_state_up": true, - "delay": 20, - "expected_codes": "200", - "http_method": "GET", - "max_retries": 3, - "timeout": 10, - "type": "HTTP", - "url_path": "/" - } - }, - "HaproxyPool": { - "Type": "OS::Neutron::Pool", - "Properties": { - "admin_state_up": true, - "description": "Haproxy pool from teplate", - "lb_method": "ROUND_ROBIN", - "monitors": [{"Ref":"HttpHM"}], - "name": "Haproxy pool", - "protocol": "HTTP", - "subnet_id": {"Ref":"Subnet"}, - "vip": { - "subnet": {"Ref":"Subnet"}, - "address": {"Ref":"vip_ip"}, - "name": "Haproxy vip", - "protocol_port": 80, - "connection_limit": -1, - "admin_state_up": true, - "description": "Haproxy vip from template" - } - } - }, - "HaproxyLb": { - "Type": "OS::Neutron::LoadBalancer", - "Properties": { - "pool_id": {"Ref":"HaproxyPool"}, - "protocol_port": 80 - } - }, - "Member1": { - "Type": "OS::Neutron::PoolMember", - "Properties": { - "address": {"Ref":"PoolMemberIP1"}, - "admin_state_up": true, - "pool_id": {"Ref":"HaproxyPool"}, - "protocol_port": 80, - "weight": 1 - } - }, - "Member2": { - "Type": "OS::Neutron::PoolMember", - "Properties": { - "address": {"Ref":"PoolMemberIP2"}, - "admin_state_up": true, - "pool_id": {"Ref":"HaproxyPool"}, - "protocol_port": 80, - "weight": 1 - } - }, - "Member3": { - "Type": "OS::Neutron::PoolMember", - "Properties": { - "address": {"Ref":"PoolMemberIP3"}, - "admin_state_up": true, - "pool_id": {"Ref":"HaproxyPool"}, - "protocol_port": 80, - "weight": 1 - } - } - } -} diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/run_suite b/gbpservice/tests/contrib/gbpfunctests/testcases/run_suite deleted file mode 100755 index d23cc2136..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/run_suite +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/bash -printf "\nWelcome to GBP Functional & Negative Test Suite\n" -echo Starting Tests `date` -sudo rm -f test_results.txt -#rc_loc=$(sudo find / -name openrc) -#echo $rc_loc -#source $rc_loc admin admin -FILES=$(>gbpteststdout - sudo python $f - ret=$? - echo "STATUS === $ret" - fi -done -printf "\n!!!!! GBP FUNCTIONAL and NEGATIVE TEST SUITE RUN HAS COMPLETED !!!!!\n" -printf "\n######### FINAL RESULTS AFTER THE COMPLETE RUN OF SUITE IS BELOW : #########\n" -NUMBER_OF_TC=$(grep -r TESTCASE_GBP_ test_results.txt | wc -l) -PASSED=$(grep -r PASSED test_results.txt | wc -l) -FAILED=$(grep -r FAILED test_results.txt | wc -l) -printf "\nTotal Number of TestCases Executed= ${NUMBER_OF_TC}\n" -printf "\nNumber of Testcases PASSED = ${PASSED}\n" -printf "\nNumber of Testcases FAILED = ${FAILED}\n" -#printf "\nSTDOUT of Suite Run = gbpteststdout.txt\n" -echo Finised Tests `date` diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/suite_admin_run.py b/gbpservice/tests/contrib/gbpfunctests/testcases/suite_admin_run.py deleted file mode 100755 index 4ab7b9e0f..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/suite_admin_run.py +++ /dev/null @@ -1,49 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import subprocess -import subprocess -import sys - - -def main(): - """ - Main: Wrapper for shared_func tests - """ - # Usage: python suite_admin_run.py - print("Functional Test Script to execute Shared Resource Testcases") - cmd_list = ["sudo sh -c 'cat /dev/null > test_results_admin.txt'", - "sudo chmod 777 test_results_admin.txt "] - for cmd in cmd_list: - subprocess.getoutput(cmd) - test_list = ['tc_gbp_pr_pc_pa_shared_func.py', - 'tc_gbp_prs_pr_shared_func.py'] - for test in test_list: - cmd = 'python %s' % (test) - print(cmd) - subprocess.call(cmd, shell=True) - results_file = open("test_results_admin.txt") - contents = results_file.read() - results_file.close() - print(contents) - print(("\n\nTotal Number of Shared Resource TestCases Executed= %s" % ( - contents.count("_SHARED_")))) - print(("\n\nNumber of TestCases Passed= %s" % (contents.count("PASSED")))) - print(("\n\nNumber of TestCases Failed= %s" % (contents.count("FAILED")))) - if contents.count("FAILED") > 0: - sys.exit(1) - else: - return 0 - - -if __name__ == "__main__": - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/suite_non_admin_run.py b/gbpservice/tests/contrib/gbpfunctests/testcases/suite_non_admin_run.py deleted file mode 100755 index 74d94461e..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/suite_non_admin_run.py +++ /dev/null @@ -1,76 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import subprocess -import sys - - -def run_func_neg(): - """ - Method to manipulate needed files - """ - # Assumption is all files are in current directory - # if 'Ubuntu' in platform.linux_distribution(): - # directory = "/usr/local/lib/python2.7/dist-packages/gbpfunctests/" - # else: - # directory = "/usr/lib/python2.7/site-packages/gbpfunctests/" # in RHEL - cmd_list = ["sudo sh -c 'cat /dev/null > test_results.txt'", - "sudo sh -c 'cat /dev/null > func_neg.txt'", - "sudo sh -c 'ls *_func*.py | grep -v shared > func_neg.txt'", - "sudo sh -c 'ls *_neg.py >> func_neg.txt'", - "sudo chmod 777 *"] - for cmd in cmd_list: - subprocess.getoutput(cmd) - return "func_neg.txt" - - -def main(): - """ - Test Execution - Generating Results - """ - usage = "Usage: python suite_run.py <'aci' or 'upstream'>" - try: - flag = sys.argv[1] - except Exception: - print(('%s' % (usage))) - sys.exit(1) - fname = run_func_neg() - num_lines = sum(1 for line in open(fname)) - print(("\nNumber of Functional Test Scripts to execute = %s" % - (num_lines))) - with open(fname) as f: - for i, l in enumerate(f, 1): - print(("Functional Test Script to execute now == %s" % l)) - # Assumption: test-scripts are executable from any location - # Reading the line from text file, also reads trailing \n, hence we - # need to strip - cmd = 'python %s %s' % (l.strip(), flag) - print(cmd) - # out=getoutput(cmd) - subprocess.call(cmd, shell=True) - f = open("test_results.txt") - contents = f.read() - f.close() - print(contents) - print(("\n\nTotal Number of TestCases Executed= %s" % ( - contents.count("TESTCASE_GBP_")))) - print(("\n\nNumber of TestCases Passed= %s" % (contents.count("PASSED")))) - print(("\n\nNumber of TestCases Failed= %s" % (contents.count("FAILED")))) - if contents.count("FAILED") > 0: - sys.exit(1) - else: - return 0 - - -if __name__ == "__main__": - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/suite_run.py b/gbpservice/tests/contrib/gbpfunctests/testcases/suite_run.py deleted file mode 100755 index 35803c364..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/suite_run.py +++ /dev/null @@ -1,69 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import subprocess -import subprocess -import sys - - -def run_func_neg(): - # Assumption is all files are in current directory - # if 'Ubuntu' in platform.linux_distribution(): - # directory = "/usr/local/lib/python2.7/dist-packages/gbpfunctests/" - # else: - # directory = "/usr/lib/python2.7/site-packages/gbpfunctests/" # in RHEL - cmd_list = ["sudo sh -c 'cat /dev/null > test_results.txt'", - "sudo sh -c 'cat /dev/null > func_neg.txt'", - "sudo sh -c 'ls *_func*.py > func_neg.txt'", - "sudo sh -c 'ls *_neg.py >> func_neg.txt'", - "sudo chmod 777 *"] - for cmd in cmd_list: - subprocess.getoutput(cmd) - return "func_neg.txt" - - -def main(): - usage = "Usage: python suite_run.py <'aci' or 'upstream'>" - try: - flag = sys.argv[1] - except Exception: - print(('%s' % (usage))) - sys.exit(1) - fname = run_func_neg() - num_lines = sum(1 for line in open(fname)) - print(("\nNumber of Functional Test Scripts to execute = %s" % num_lines)) - with open(fname) as f: - for i, l in enumerate(f, 1): - print(("Functional Test Script to execute now == %s" % l)) - # Assumption: test-scripts are executable from any location - # Reading the line from text file, also reads trailing \n, hence we - # need to strip - cmd = 'python %s %s' % (l.strip(), flag) - print(cmd) - # out=getoutput(cmd) - subprocess.call(cmd, shell=True) - f = open("test_results.txt") - contents = f.read() - f.close() - print(contents) - print(("\n\nTotal Number of TestCases Executed= %s" % ( - contents.count("TESTCASE_GBP_")))) - print(("\n\nNumber of TestCases Passed= %s" % (contents.count("PASSED")))) - print(("\n\nNumber of TestCases Failed= %s" % (contents.count("FAILED")))) - if contents.count("FAILED") > 0: - sys.exit(1) - else: - return 0 - - -if __name__ == "__main__": - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_l2p_func.py b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_l2p_func.py deleted file mode 100755 index aee4717fc..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_l2p_func.py +++ /dev/null @@ -1,311 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import subprocess -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs - - -def main(): - - # Run the Testcases: - test = test_gbp_l2p_func() - if test.test_gbp_l2p_func_1() == 0: - test.cleanup(tc_name='TESTCASE_GBP_L2P_FUNC_1') - if test.test_gbp_l2p_func_2() == 0: - test.cleanup(tc_name='TESTCASE_GBP_L2P_FUNC_2') - test.cleanup() - utils_libs.report_results('test_gbp_l2p_func', 'test_results.txt') - sys.exit(1) - - -class test_gbp_l2p_func(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_l2p_func.log' - subprocess.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_l2p_func.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self._log.info("\n## START OF GBP L3_POLICY FUNCTIONALITY TESTSUITE\n") - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.l3p_name = 'demo_l3p' - self.l2p_name = 'demo_l2p' - - def cleanup(self, tc_name=''): - if tc_name != '': - self._log.info('%s: FAILED' % (tc_name)) - for obj in ['group', 'l2p', 'l3p']: - self.gbpcfg.gbp_del_all_anyobj(obj) - - def test_gbp_l2p_func_1(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_L2P_FUNC_1: TO CREATE/VERIFY/DELETE/VERIFY " - "a L2POLICY with DEFAULT ATTRIB VALUE\n" - "TEST_STEPS::\n" - "Create L2 Policy Object with default attributes\n" - "Verify the attributes & value, show & list cmds\n" - "Verify the implicit neutron objects and gbp L3P\n" - "Delete L2 Policy\n" - "Verify that L2P,L3P and implicit neutron objects has got " - "deleted, show & list cmds\n" - "##############################################################\n") - - # Testcase work-flow starts - # Create and Verify L2Policy with default attrs(L3Policy & implicit - # Neutron net obj) - self._log.info( - '\n## Step 1: Create L2Policy with default attrib vals##\n') - uuids = self.gbpcfg.gbp_policy_cfg_all(1, 'l2p', self.l2p_name) - if uuids == 0: - self._log.info("\n## Step 1: Create L2Policy == Failed") - return 0 - elif len(uuids) < 2: - self._log.info( - "\n## Step 1:Create L2Policy Failed due to unexpected " - "tuple length\n") - return 0 - else: - l2p_uuid, def_l3p_uuid = uuids[0], uuids[1] - self._log.info( - "\n## Step 2: Verify L2Policy, default L3Policy, and Implicit " - "Neutron objs") - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 0, 'l2p', l2p_uuid, self.l2p_name) == 0: - self._log.info( - "\n## Step 2A: Verify L2Policy using -list option == Failed") - return 0 - net_uuid = self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, - 'l2p', - l2p_uuid, - ret='default', - id=l2p_uuid, - name=self.l2p_name, - l3_policy_id=def_l3p_uuid)[1] - if net_uuid != 0 and isinstance(net_uuid, str): - if self.gbpverify.neut_ver_all( - 'net', - net_uuid, - name='l2p_%s' % - (self.l2p_name), - admin_state_up='True', - status='ACTIVE', - shared='False') == 0: - self._log.info( - "\n## Step 2C: Verify implicit neutron network " - "object == Failed") - return 0 - else: - self._log.info( - "\n## Step 2B: Verify L2Policy using -show option == Failed") - return 0 - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, - 'l3p', - def_l3p_uuid, - name='default', - id=def_l3p_uuid, - l2_policies=l2p_uuid) == 0: - self._log.info("\n## Step 2D: Verify default L3Policy == Failed") - return 0 - - # Delete the L2Policy and verify L2P,def l3P and Neutron obj are - # deleted too - self._log.info( - '\n## Step 3: Delete L2Policy and Verify L2P, default L3P and ' - 'Implicit Neutron deleted ##\n') - if self.gbpcfg.gbp_policy_cfg_all(0, 'l2p', self.l2p_name) == 0: - self._log.info("\n## Step 3: Delete L2Policy == Failed") - return 0 - if self.gbpverify.gbp_l2l3ntk_pol_ver_all(1, 'l2p', l2p_uuid) != 0: - self._log.info( - "\n## Step 3A: Verify L2Policy is Deleted using -show " - "option == Failed") - return 0 - if self.gbpverify.gbp_l2l3ntk_pol_ver_all(1, 'l3p', def_l3p_uuid) != 0: - self._log.info( - "\n## Step 3B: Verify default L3Policy is Deleted == Failed") - return 0 - if self.gbpverify.neut_ver_all('net', net_uuid) != 0: - self._log.info( - "\n## Step 3C: Verify Implicit Neutron Network Obj is " - "Deleted == Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_L2P_FUNC_1: PASSED") - return 1 - - def test_gbp_l2p_func_2(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_L2P_FUNC_2: TO CREATE/UPDATE/DELETE/VERIFY a " - "L2POLICY with MULTIPLE PTGs\n" - "TEST_STEPS::\n" - "Create L2Policy Object with non-default params\n" - "Verify the attributes & value, show & list cmds\n" - "Create Multiple(n=10) PTGs using the above L2P\n" - "Verify the PTGs and L2P are reflecting in each other in the DB\n" - "Delete all PTG, L2P\n" - "Verify that all Implicit Neutron Net/Subnets, default L3P has " - "got deleted along with PTGs & L2P\n" - "##############################################################\n") - - # Testcase work-flow starts - # Create and Verify L2Policy with default attrs(L3Policy & implicit - # Neutron net obj) - self._log.info( - '\n## Step 1: Create L2Policy with default attrib vals##\n') - uuids = self.gbpcfg.gbp_policy_cfg_all(1, 'l2p', self.l2p_name) - if uuids == 0: - self._log.info("\n## Step 1: Create L2Policy == Failed") - return 0 - elif len(uuids) < 2: - self._log.info( - "\n## Step 1:Create L2Policy Failed due to unexpected " - "tuple length\n") - return 0 - else: - l2p_uuid, def_l3p_uuid = uuids[0], uuids[1] - self._log.info( - "\n## Step 2: Verify L2Policy, default L3Policy, and Implicit " - "Neutron objs") - net_uuid = self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, - 'l2p', - l2p_uuid, - ret='default', - id=l2p_uuid, - name=self.l2p_name, - l3_policy_id=def_l3p_uuid)[1] - if net_uuid != 0 and isinstance(net_uuid, str): - if self.gbpverify.neut_ver_all( - 'net', - net_uuid, - name='l2p_%s' % - (self.l2p_name), - admin_state_up='True', - status='ACTIVE', - shared='False') == 0: - self._log.info( - "\n## Step 2A: Verify implicit neutron network " - "object == Failed") - return 0 - else: - self._log.info( - "\n## Step 2B: Verify L2Policy using -show option == Failed") - return 0 - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, - 'l3p', - def_l3p_uuid, - name='default', - id=def_l3p_uuid, - l2_policies=l2p_uuid) == 0: - self._log.info("\n## Step 2C: Verify default L3Policy == Failed") - return 0 - # Create Multiple PTGs and associate the above L2P - self._log.info( - "\n## Step 3: Create Multiple(n=10) PTGs using the above L2P\n") - n, i = 11, 1 - ptg_list = [] - subnet_list = [] - while i < n: - ptg_name = 'demo_ptg_%s' % (i) - uuids = self.gbpcfg.gbp_policy_cfg_all( - 1, 'group', ptg_name, l2_policy=l2p_uuid) - if uuids != 0: - ptg_list.append(uuids[0]) - subnet_list.append(uuids[2]) - else: - self._log.info("\n## Step 3: Create Target-Group == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, 'group', ptg_name, id=ptg_list[ - i - 1], shared='False', l2_policy_id=l2p_uuid) == 0: - self._log.info( - "\n## Step 3A: Verify Policy Target-Group " - "using L2P == Failed") - return 0 - if self.gbpverify.neut_ver_all( - 'subnet', subnet_list[ - i - 1], network_id=net_uuid) == 0: - self._log.info( - "\n## Step 3B: Verify Implicit Neutron Subnet == Failed") - return 0 - i += 1 - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'l2p', l2p_uuid, 'policy_target_groups', ptg_list) == 0: - self._log.info( - "\n## Step 3C: Verifying multiple PTGs mapped to " - "L2P == Failed \n") - return 0 - # Delete all PTGs and L2P - self._log.info("\n## Step 4: Delete all PTGs and L2P\n") - for ptgid in ptg_list: - if self.gbpcfg.gbp_policy_cfg_all(0, 'group', ptgid) == 0: - self._log.info("\n## Step 4: Delete Target-Group == Failed") - return 0 - if self.gbpcfg.gbp_policy_cfg_all(0, 'l2p', l2p_uuid) == 0: - self._log.info("\n## Step 4A: Delete L2Policy == Failed") - return 0 - # Verify that all Implicit Neutron Objs, Default L3p , PTGs and L2P are - # deleted - self._log.info( - "\n## Step 5: Verify that all Implicit Neutron Objs, Default L3p, " - "PTGs and L2P are deleted\n") - if self.gbpverify.gbp_l2l3ntk_pol_ver_all(1, 'l3p', 'default') != 0: - self._log.info( - "\n## Step 5: Verify that default L3P has got auto-deleted " - "== Failed\n") - return 0 - if self.gbpverify.gbp_l2l3ntk_pol_ver_all(1, 'l2p', l2p_uuid) != 0: - self._log.info( - "\n## Step 5A: Verify that L2P got deleted == Failed\n") - return 0 - for subnet in subnet_list: - if self.gbpverify.neut_ver_all('subnet', subnet) != 0: - self._log.info( - "\n## Step 5B: Verify that Implicit Neutron Subnet got " - "deleted == Failed\n") - return 0 - for ptgid in ptg_list: - if self.gbpverify.gbp_policy_verify_all(1, 'group', ptgid) != 0: - self._log.info( - "\n## Step 3B: Verify Target-Group is Deleted using " - "-show option == Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_L2P_FUNC_2: PASSED") - return 1 - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_l3p_func.py b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_l3p_func.py deleted file mode 100755 index 44f5dfe1a..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_l3p_func.py +++ /dev/null @@ -1,462 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import platform -import subprocess -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs - - -def main(): - - # Run the Testcases: - test = test_gbp_l3p_func() - if test.test_gbp_l3p_func_1() == 0: - test.cleanup(tc_name='TESTCASE_GBP_L3P_FUNC_1') - if test.test_gbp_l3p_func_2() == 0: - test.cleanup(tc_name='TESTCASE_GBP_L3P_FUNC_2') - if test.test_gbp_l3p_func_3() == 0: - test.cleanup(tc_name='TESTCASE_GBP_L3P_FUNC_3') - if test.test_gbp_l3p_func_4() == 0: - test.cleanup(tc_name='TESTCASE_GBP_L3P_FUNC_4') - test.cleanup() - utils_libs.report_results('test_gbp_l3p_func', 'test_results.txt') - sys.exit(1) - - -class test_gbp_l3p_func(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_l3p_func.log' - subprocess.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_l3p_func.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self._log.info("\n## START OF GBP L3_POLICY FUNCTIONALITY TESTSUITE\n") - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.l3p_name = 'demo_l3p' - self.l2p_name = 'demo_l2p' - - def cleanup(self, tc_name=''): - if tc_name != '': - self._log.info('## %s: FAILED' % (tc_name)) - for obj in ['group', 'l2p', 'l3p']: - self.gbpcfg.gbp_del_all_anyobj(obj) - - def test_gbp_l3p_func_1( - self, - name_uuid='', - l3p_uuid='', - rep_cr=0, - rep_del=0): - - if rep_cr == 0 and rep_del == 0: - self._log.info( - "\n########################################################\n" - "TESTCASE_GBP_L3P_FUNC_1: TO CREATE/VERIFY/DELETE/VERIFY a " - "L3POLICY with DEFAULT ATTRIB VALUE\n" - "TEST_STEPS::\n" - "Create L3 Policy Object\n" - "Verify the attributes & value, show & list cmds\n" - "Verify the implicit neutron objects\n" - "Delete L3 Policy Object\n" - "Verify that PR and implicit neutron objects has got " - "deleted, show & list cmds\n" - "##########################################################\n") - - if name_uuid == '': - name_uuid = self.l3p_name - # Testcase work-flow starts - if rep_cr == 0 or rep_cr == 1: - self._log.info( - '\n## Step 1: Create L3Policy with default attrib vals##\n') - l3p_uuid = self.gbpcfg.gbp_policy_cfg_all(1, 'l3p', name_uuid) - if l3p_uuid == 0: - self._log.info("\n## Step 1: Create L3Policy == Failed") - return 0 - # default subnet= 10.0.0.0/8 & subnet_prefix_length= 24 - self._log.info('# Step 2A: Verify L3Policy using -list cmd') - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 0, 'l3p', l3p_uuid, name_uuid, '10.0.0.0/8', '24') == 0: - self._log.info( - "\n## Step 2A: Verify L3Policy using -list option " - "== Failed") - return 0 - self._log.info('# Step 2B: Verify L3Policy using -show cmd') - if 'Ubuntu' in platform.linux_distribution(): # Only for devstack - rtr_uuid = self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, - 'l3p', - l3p_uuid, - ret='default', - id=l3p_uuid, - name=name_uuid, - ip_pool='10.0.0.0/8', - subnet_prefix_length='24', - ip_version='4') - if rtr_uuid != 0 and isinstance(rtr_uuid, str): - rtr_name = 'l3p_%s' % (name_uuid) - if self.gbpverify.neut_ver_all( - 'router', - rtr_uuid, - name=rtr_name, - admin_state_up='True', - status='ACTIVE') == 0: - self._log.info( - "\n## Step 2D: Verify L3Policy using -show " - "option == Failed") - return 0 - else: - self._log.info( - "\n## Step 2C: Verify L3Policy using -show " - "option == Failed") - return 0 - else: - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, - 'l3p', - l3p_uuid, - id=l3p_uuid, - name=name_uuid, - ip_pool='10.0.0.0/8', - subnet_prefix_length='24', - ip_version='4') == 0: - self._log.info( - "\n## Step 2C: Verify L3Policy using -show " - "option == Failed") - return 0 - ####### - if rep_del == 0 or rep_del == 1: - self._log.info('\n## Step 3: Delete L3Policy using name ##\n') - if self.gbpcfg.gbp_policy_cfg_all(0, 'l3p', name_uuid) == 0: - self._log.info("\n## Step 3: Delete L3Policy == Failed") - return 0 - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 0, 'l3p', name_uuid, l3p_uuid) != 0: - self._log.info( - "\n## Step 3A: Verify L3Policy is Deleted using " - "-list option == Failed") - return 0 - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, 'l3p', name_uuid, l3p_uuid) != 0: - self._log.info( - "\n## Step 3B: Verify L3Policy is Deleted using " - "-show option == Failed") - return 0 - if rep_cr == 0 and rep_del == 0: - self._log.info("\n## TESTCASE_GBP_L3P_FUNC_1: PASSED") - return 1 - - def test_gbp_l3p_func_2(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_L3P_FUNC_2: TO CREATE/UPDATE/DELETE/VERIFY a " - "L3POLICY with EDITABLE ATTRs\n" - "TEST_STEPS::\n" - "Create L3Policy Object with non-default params\n" - "Verify the attributes & value, show & list cmds\n" - "Update the L3Policy Objects\n" - "Verify the attributes & value, show & list cmds\n" - "Delete L3Policy using Name\n" - "Verify that L3P has got deleted, show & list cmds\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info( - "\n## Step 1: Create Policy L3Policy with non-default " - "attrs and values ##") - l3p_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'l3p', self.l3p_name, ip_pool='20.20.0.0/24', - subnet_prefix_length='28') - if l3p_uuid == 0: - self._log.info("\n## Step 1: Create L3Policy == Failed") - return 0 - self._log.info('\n## Step 2B: Verify L3Policy using -show cmd') - if 'Ubuntu' in platform.linux_distribution(): # Only for devstack - rtr_uuid = self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, - 'l3p', - l3p_uuid, - ret='default', - id=l3p_uuid, - name=self.l3p_name, - ip_pool='20.20.0.0/24', - subnet_prefix_length='28', - ip_version='4') - if rtr_uuid != 0 and isinstance(rtr_uuid, str): - rtr_name = 'l3p_%s' % (self.l3p_name) - if self.gbpverify.neut_ver_all( - 'router', - rtr_uuid, - name=rtr_name, - admin_state_up='True', - status='ACTIVE') == 0: - self._log.info( - "\n## Step 2D: Verify L3Policy using -show option" - " == Failed") - return 0 - else: - self._log.info( - "\n## Step 2C: Verify L3Policy using -show option" - " == Failed") - return 0 - else: - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, - 'l3p', - l3p_uuid, - id=l3p_uuid, - name=self.l3p_name, - ip_pool='20.20.0.0/24', - subnet_prefix_length='28', - ip_version='4') == 0: - self._log.info( - "\n## Step 2C: Verify L3Policy using -show option" - " == Failed") - - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'l3p', self.l3p_name, subnet_prefix_length='26') == 0: - self._log.info( - "\n## Step 3: UPdating L3Policy attributes == Failed") - return 0 - self._log.info( - "\n## Step 3: Verify that Updated Attributes in L3Policy") - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, - 'l3p', - self.l3p_name, - id=l3p_uuid, - name=self.l3p_name, - ip_pool='20.20.0.0/24', - subnet_prefix_length='26', - ip_version='4') == 0: - self._log.info( - "\n## Step 3: Verify L3Policy using -show option == Failed") - self.test_gbp_l3p_func_1(name_uuid=l3p_uuid, rep_cr=2) - self._log.info("\n## TESTCASE_GBP_L3P_FUNC_2: PASSED") - return 1 - - def test_gbp_l3p_func_3(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_L3P_FUNC_3: TO CREATE/UPDATE/DELETE/VERIFY " - "L3POLICY AND ASSOCIATED L2POLICY\n" - "TEST_STEPS::\n" - "Create L3Policy with defined attributes\n" - "Create L2Policy with default attributes\n" - "Update L2Policy to change the from default to the above " - "non-default L3Policy\n" - "Verify the Update of L3Policy attribute of L2Policy fails\n" - "Update L3Policy(default) editable attributes\n" - "Delete the L2Policy(this causes auto-delete of default-L3Pol)\n" - "Verify L3/L2Policies successfully deleted\n" - "##############################################################\n") - - # Testcase work-flow starts - # Create L2 L3 Policy - self._log.info( - "\n## Step 1: Create L3Policy with non-default attrs and " - "values ##") - l3p_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'l3p', self.l3p_name, ip_pool='20.20.0.0/24', - subnet_prefix_length='28', - proxy_ip_pool='192.167.0.0/16') - if l3p_uuid == 0: - self._log.info("\n## Step 1: Create L3Policy == Failed") - return 0 - self._log.info( - '\n## Step 1A: Create L2Policy with default attributes##\n') - l2p = self.gbpcfg.gbp_policy_cfg_all(1, 'l2p', self.l2p_name) - if l2p == 0: - self._log.info( - "\n## New L2Policy Create Failed, hence " - "Testcase_gbp_l3p_func_3 ABORTED\n") - return 0 - elif len(l2p) < 2: - self._log.info( - "\n## New L2Policy Create Failed due to " - "unexpected tuple length\n") - return 0 - else: - l2p_uuid, def_l3p_uuid = l2p[0], l2p[1] - # Associating L2Policy with non-default L3Policy(should Fail) and - # UPdating the L3Policy(in-use/default) - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'l2p', self.l2p_name, l3_policy_id=l3p_uuid) != 0: - self._log.info( - "\n## Updating/Changing L3Policy attribute of " - "L2Policy did NOT Fail") - return 0 - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, 'l2p', self.l2p_name, l3_policy_id=def_l3p_uuid) == 0: - self._log.info( - "\n## Step 3A: Verify L2Policy is still associated to " - "its default L3Policy == Failed") - return 0 - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'l3p', def_l3p_uuid, subnet_prefix_length='27') == 0: - self._log.info( - "\n## Step 4: UPdating default L3Policy's " - "attributes == Failed") - return 0 - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, - 'l3p', - def_l3p_uuid, - id=def_l3p_uuid, - ip_pool='10.0.0.0/8', - l2_policies=l2p_uuid, - subnet_prefix_length='27', - ip_version='4') == 0: - self._log.info( - "\n## Step 4A: Verify L3Policy after associating " - "to the L2Policy == Failed") - return 0 - - # Delete L2/L3 Policies - if self.gbpcfg.gbp_policy_cfg_all(0, 'l2p', l2p_uuid) == 0: - self._log.info("\n## Step 5: Delete L2Policy == Failed") - return 0 - if self.gbpverify.gbp_l2l3ntk_pol_ver_all(1, 'l2p', l2p_uuid) != 0: - self._log.info("\n## Step 5A: Verify Delete of L2Policy == Failed") - return 0 - if self.gbpverify.gbp_l2l3ntk_pol_ver_all(1, 'l3p', def_l3p_uuid) != 0: - self._log.info( - "\n## Step 5B: Verify Auto-Delete of default " - "L3Policy == Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_L3P_FUNC_3: PASSED") - return 1 - - def test_gbp_l3p_func_4(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_L3P_FUNC_4: TO CREATE/UPDATE/DELETE/VERIFY " - "MULTI L2POLICY to SINGLE L3POLICY\n" - "TEST_STEPS::\n" - "Create non-default L3Policy with defined attributes\n" - "Create Multiple L2Policies with above non-default L3policy\n" - "Verify that L2Policies are created with non-default L3Policy\n" - "Delete all L2 Policies\n" - "Verify that non-default L3 Policy exists but with null " - "L2Policies\n" - "Delete the L3Policy\n" - "Verify L3/L2Policys successfully deleted\n" - "##############################################################\n") - - # Testcase work-flow starts - # Create and Verify non-default L3 Policy - self._log.info( - "\n## Step 1: Create Policy L3Policy with non-default " - "attrs and values ") - l3p_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'l3p', self.l3p_name, ip_pool='40.50.0.0/16', - subnet_prefix_length='25') - if l3p_uuid == 0: - self._log.info("\n## Step 1: Create L3Policy == Failed") - return 0 - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, - 'l3p', - l3p_uuid, - id=l3p_uuid, - name=self.l3p_name, - ip_pool='40.50.0.0/16', - subnet_prefix_length='25', - ip_version='4') == 0: - self._log.info("\n## Step 1A: Verify non-default == Failed") - return 0 - # Create and verify multiple L2 policy with above non-default L3P - self._log.info( - "\n## Step 2: Create and Verify multiple(n=10) L2Policy " - "associated with 1 non-default L3P") - l2p_uuid_list = [] - n, i = 11, 1 - while i < n: - l2p_name = 'demo_l2p_%s' % (i) - l2p = self.gbpcfg.gbp_policy_cfg_all( - 1, 'l2p', l2p_name, l3_policy_id=l3p_uuid) - if l2p == 0: - self._log.info( - "\n## Step 2B:New L2Policy Create Failed, hence " - "Testcase_gbp_l3p_func_4 ABORTED\n") - return 0 - elif len(l2p) < 2: - self._log.info( - "\n## Step 2C: New L2Policy Create Failed due to " - "unexpected tuple length\n") - return 0 - else: - l2p_uuid = l2p[0] - l2p_uuid_list.append(l2p_uuid) - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, 'l2p', l2p_name, id=l2p_uuid, - l3_policy_id=l3p_uuid) == 0: - self._log.info( - "\n## Step 2D: Verify L2Policy using non-default " - "L3P == Failed") - return 0 - i += 1 - # Verify that non-default L3P has all the above create L2Ps - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'l3p', l3p_uuid, 'l2_policies', l2p_uuid_list) == 0: - self._log.info( - "\n## Step 2E: Verifying multiple L2Ps mapped to " - "non-default L3P == Failed \n") - return 0 - # Delete all L2Ps and verify that non-default L3P has null L2Ps - self._log.info( - "\n## Step 3: Delete all L2Ps and verify that non-default " - "L3P has no L2P associated\n") - for l2pid in l2p_uuid_list: - if self.gbpcfg.gbp_policy_cfg_all(0, 'l2p', l2pid) == 0: - self._log.info( - "\n## Step 3: Delete of L2P %s == Failed\n" % - (l2pid)) - return 0 - if self.gbpverify.gbp_l2l3ntk_pol_ver_all(1, 'l2p', l2pid) != 0: - self._log.info( - "\n## Step 3A: Verify that L2P got deleted == Failed\n") - return 0 - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'l3p', l3p_uuid, 'l2_policies', l2p_uuid_list) != 0: - self._log.info( - "\n## Step 3B: Verifying Non-Default L3P has no more " - "L2P mapped == Failed \n") - return 0 - self.test_gbp_l3p_func_1(name_uuid=l3p_uuid, rep_cr=2) - self._log.info("\n## TESTCASE_GBP_L3P_FUNC_4: PASSED") - return 1 - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_l3p_neg.py b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_l3p_neg.py deleted file mode 100755 index 6cef2dce9..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_l3p_neg.py +++ /dev/null @@ -1,307 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import subprocess -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs -import six - - -def main(): - - # Run the Testcases: - test = test_gbp_l3p_neg() - if test.test_gbp_l3p_neg_1() == 0: - test.cleanup(tc_name='TESTCASE_GBP_L3P_NEG_1') - if test.test_gbp_l3p_neg_2() == 0: - test.cleanup(tc_name='TESTCASE_GBP_L3P_NEG_2') - if test.test_gbp_l3p_neg_3() == 0: - test.cleanup(tc_name='TESTCASE_GBP_L3P_NEG_3') - if test.test_gbp_l3p_neg_4() == 0: - test.cleanup(tc_name='TESTCASE_GBP_L3P_NEG_4') - if test.test_gbp_l3p_neg_5() == 0: - test.cleanup(tc_name='TESTCASE_GBP_L3P_NEG_5') - test.cleanup() - utils_libs.report_results('test_gbp_l3p_neg', 'test_results.txt') - sys.exit(1) - - -class test_gbp_l3p_neg(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_l3p_neg.log' - subprocess.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_l3p_neg.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self._log.info("\n## START OF GBP L3_POLICY NEGATIVE TESTSUITE\n") - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.l3p_name = 'demo_l3p' - - def cleanup(self, tc_name=''): - if tc_name != '': - self._log.info('%s: FAILED' % (tc_name)) - for obj in ['group', 'l2p', 'l3p']: - self.gbpcfg.gbp_del_all_anyobj(obj) - - def test_gbp_l3p_neg_1(self): - - self._log.info( - "\n#############################################\n" - "TESTCASE_GBP_L3P_NEG_1: TO CREATE/VERIFY L3POLICY " - "with INVALID IP-POOL\n" - "TEST_STEPS::\n" - "Create L3Policy Object with Invalid IP-Pool\n" - "Invalid IP-Pools: x.y.0.0/24, 0.0.0.0/0,255.255.255.255/32," - "0.2323.0.0/24\n" - "Verify the create FAILs and config rolls back\n" - "############################################\n") - - # Testcase work-flow starts - count = 0 - invalid_pools = [ - 'x.y.0.0/24', - '0.2323.0.0/24', - '0.0.0.0/0', - '255.255.255.255/32'] - for pool in invalid_pools: - self._log.info( - "\n## Step 1A: Create L3Policy with Invalid IP-Pool = %s ##" % - (pool)) - if self.gbpcfg.gbp_policy_cfg_all( - 1, 'l3p', self.l3p_name, ip_pool=pool) != 0: - self._log.info( - "# Step 1A: Create L3Policy with Invalid IP-Pool %s did " - "NOT fail" % - (pool)) - self._log.info('# Step 1A: Verify L3Policy did NOT get created') - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, 'l3p', self.l3p_name) != 0: - self._log.info( - "# Step 1A: L3Policy did NOT fail to create even with " - "Invalid IP-Pool %s" % - (pool)) - count += 1 - if count > 0: - return 0 - else: - self._log.info("\nTESTCASE_GBP_L3P_NEG_1: PASSED") - return 1 - - def test_gbp_l3p_neg_2(self): - - self._log.info( - "\n############################################\n" - "TESTCASE_GBP_L3P_NEG_2: TO CREATE/VERIFY L3POLICY with INVALID " - "SUBNET-PREF-LENGTH\n" - "TEST_STEPS::\n" - "Create L3Policy Object with Invalid Subnet-Prefix-Length\n" - "Invalid Subnet-Prefix-Lengths: 33,'AB','32'\n" - "Verify the create FAILs and config rolls back\n" - "############################################\n") - - # Testcase work-flow starts - cnt = 0 - invalid_prefix_length = ['33', 'AB', '32'] - for prefix in invalid_prefix_length: - self._log.info( - "\n## Step 1A: Create L3Policy with Invalid " - "Prefix-lenght = %s ##" % - (prefix)) - if self.gbpcfg.gbp_policy_cfg_all( - 1, 'l3p', self.l3p_name, subnet_prefix_length=prefix) != 0: - self._log.info( - "# Step 1A: Create L3Policy with Invalid IP-Pool %s " - "did NOT fail" % - (prefix)) - self._log.info('# Step 1A: Verify L3Policy did NOT get created') - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, 'l3p', self.l3p_name) != 0: - self._log.info( - "# Step 1A: L3Policy did NOT fail to create even with " - "Invalid IP-Pool %s" % - (prefix)) - cnt += 1 - if cnt > 0: - return 0 - else: - self._log.info("\nTESTCASE_GBP_L3P_NEG_2: PASSED") - return 1 - - def test_gbp_l3p_neg_3(self): - - self._log.info( - "\n############################################\n" - "TESTCASE_GBP_L3P_NEG_3: TO CREATE/VERIFY L3POLICY with mix " - "of VALID & INVALID ATTRs\n" - "TEST_STEPS::\n" - "Create L3Policy with a mix of Valid IP-Pool and Invalid " - "Subnet-Prefix-Length & Vice-versa\n" - "Invalid IP-Pool: x.y.0.0/24,Valid Subnet-Pref-Len: 30\n" - "Valid IP-Pool: 20.20.20.0/24, Invalid Subnet-Pref-Len: 32\n" - "Verify the create FAILs and config rolls back\n" - "############################################\n") - - # Testcase work-flow starts - mix_attr = {'x.y.0.0/24': '30', '20.20.20.0/24': '32'} - _pass = 0 - for ip, pref in six.iteritems(mix_attr): - self._log.info( - "\n## Step 1A: Create L3Policy with IP-Pool = %s & " - "Subnet-Pref-Len = %s ##" % - (ip, pref)) - if self.gbpcfg.gbp_policy_cfg_all( - 1, - 'l3p', - self.l3p_name, - ip_pool=ip, - subnet_prefix_length=pref) != 0: - self._log.info( - "# Step 1A: Create L3Policy with mix of valid and " - "invalid did NOT fail") - self._log.info('# Step 1A: Verify L3Policy did NOT get created') - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, 'l3p', self.l3p_name) != 0: - self._log.info( - "# Step 1A: L3Policy did NOT fail to create even with " - "mix of Valid and Invalid attrs %s") - _pass += 1 - if _pass > 0: - return 0 - else: - self._log.info("\nTESTCASE_GBP_L3P_NEG_3: PASSED") - return 1 - - def test_gbp_l3p_neg_4(self): - - self._log.info( - "\n#################################################\n" - "TESTCASE_GBP_L3P_NEG_4: TO UPDATE/VERIFY L3POLICY with " - "INVALID ATTRs\n" - "TEST_STEPS::\n" - "Create a L3Policy with default attr values\n" - "Update the L3Policy with Invalid Subnet-Prefix-Length\n" - "Update the L3Policy with Valid IP-Pool, should fail as " - "ip-pool is Immutable attr\n" - "Verify the update fails and config roll backs to original " - "values of the L3Policy\n" - "###############################################\n") - - # Testcase work-flow starts - self._log.info('\n## Step 1: Create a L3P with default attribute ##\n') - l3p_uuid = self.gbpcfg.gbp_policy_cfg_all(1, 'l3p', self.l3p_name) - if l3p_uuid == 0: - self._log.info("\n## Step 1: Create L3Policy == Failed") - return 0 - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'l3p', l3p_uuid, subnet_prefix_length='32') != 0: - self._log.info( - "\n## Step 2: Updating L3Policy's Subnet-Prefix-Length " - "with Invalid Value=32 did NOT fail") - return 0 - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'l3p', l3p_uuid, ip_pool='20.20.0.0/24') != 0: - self._log.info( - "\n## Step 3: Updating L3Policy's Immutable attr IP-Pool " - "did NOT fail") - return 0 - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, - 'l3p', - l3p_uuid, - id=l3p_uuid, - name=self.l3p_name, - ip_pool='10.0.0.0/8', - subnet_prefix_length='24') == 0: - self._log.info( - "\n## Step 4: L3Policy config did NOT roll back to original " - "default values") - return 0 - self.gbpcfg.gbp_policy_cfg_all( - 0, 'l3p', l3p_uuid) # clean-up before next testcase - self._log.info("\nTESTCASE_GBP_L3P_NEG_4: PASSED") - return 1 - - def test_gbp_l3p_neg_5(self): - - self._log.info( - "\n#################################################\n" - "TESTCASE_GBP_L3P_NEG_5: TO CREATE/UPDATE L3POLICY with " - "SUBNET-PREF-LENGTH GREATER than IP-POOL's MASK-LENGTH\n" - "TEST_STEPS::\n" - "Create a L3Policy with non-default attr, " - "subnet-pref-length > mask-length of pool\n" - "Verify the above L3Policy creation fails\n" - "Create a L3Policy with default attrs\n" - "Update the L3Policy's subnet-pref-length such that " - "subnet-pref-length > mask-length of pool\n" - "Verify the update fails and L3Policy attrs persists with " - "default values\n" - "##################################################\n") - - # Testcase work-flow starts - self._log.info('\n## Step 1: Create a L3P with default attribute ##\n') - l3p_uuid = self.gbpcfg.gbp_policy_cfg_all(1, 'l3p', self.l3p_name) - if l3p_uuid == 0: - self._log.info("\n## Step 1: Create L3Policy == Failed") - return 0 - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'l3p', l3p_uuid, subnet_prefix_length='4') != 0: - self._log.info( - "\n## Step 2: Updating L3Policy's " - "Subnet-Prefix-Length > default Mask-length(8) did NOT fail") - return 0 - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, - 'l3p', - l3p_uuid, - id=l3p_uuid, - name=self.l3p_name, - ip_pool='10.0.0.0/8', - subnet_prefix_length='24') == 0: - self._log.info( - "\n## Step 3: L3Policy config did NOT roll back " - "to original default values") - return 0 - if self.gbpcfg.gbp_policy_cfg_all( - 1, - 'l3p', - 'new_l3p', - ip_pool='20.20.20.0/24', - subnet_prefix_length='16') != 0: - self._log.info( - "\n## Step 4: Creating L3Policy with " - "Subnet-Prefix-Length > Mask-Length(24) did NOT fail") - return 0 - self._log.info("\nTESTCASE_GBP_L3P_NEG_5: PASSED") - return 1 - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_nsp_func.py b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_nsp_func.py deleted file mode 100755 index 02ea434df..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_nsp_func.py +++ /dev/null @@ -1,442 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import subprocess -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs - - -def main(): - - # Run the Testcases: - test = test_gbp_nsp_func() - if test.test_gbp_nsp_func_1() == 0: - test.cleanup(tc_name='TESTCASE_GBP_NSP_FUNC_1') - if test.test_gbp_nsp_func_2() == 0: - test.cleanup(tc_name='TESTCASE_GBP_NSP_FUNC_2') - if test.test_gbp_nsp_func_3() == 0: - test.cleanup(tc_name='TESTCASE_GBP_NSP_FUNC_3') - test.cleanup() - utils_libs.report_results('test_gbp_nsp_func', 'test_results.txt') - sys.exit(1) - - -class test_gbp_nsp_func(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_nsp_func.log' - subprocess.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_nsp_func.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self._log.info( - "\n## START OF GBP NETWORK_SERVICE_POLICY FUNCTIONALITY " - "TESTSUITE\n") - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.nsp_name = 'demo_nsp' - - def cleanup(self, tc_name=''): - if tc_name != '': - self._log.info('%s: FAILED' % (tc_name)) - for obj in ['group', 'nsp']: - self.gbpcfg.gbp_del_all_anyobj(obj) - - def test_gbp_nsp_func_1(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_NSP_FUNC_1: TO CREATE/REFER/DELETE/VERIFY " - "NTK-SVC-POLICY in PTG\n" - "TEST_STEPS::\n" - "Create two NSPs one with type:ip-pool & ip-single, " - "value:self_subnet and self_subnet\n" - "Verify the attributes & values\n" - "Create two PTGs and reference each one of the above " - "NSP in one of the PTG\n" - "Verify the NSP reference in the PTGs\n" - "Delete the PTG and the NSP\n" - "Verify that NSP got deleted\n" - "##############################################################\n") - - # Testcase work-flow starts - # Create and Verify NSPolicy with type=ip_single & ip-single, - # name:self_subnet & self_subnet - self._log.info( - '\n## Step 1: Create NSPolicy with type=ip_single & ' - 'name:self_subnet ##\n') - nsp1_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'nsp', - 'demo_nsp_1', - network_service_params="type=ip_single,name=vip_ip1," - "value=self_subnet") - if nsp1_uuid == 0: - self._log.info( - "\n## Step 1A: Create NSPolicy with type=ip_single & " - "name:self_subnet == Failed") - return 0 - nsp2_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'nsp', - 'demo_nsp_2', - network_service_params="type=ip_single,name=vip_ip2," - "value=self_subnet") - if nsp2_uuid == 0: - self._log.info( - "\n## Step 1B: Create NSPolicy with type=ip_single & " - "name:self_subnet == Failed") - return 0 - # Verify - self._log.info( - "\n## Step 2: Verify NSPolicies are successfully created") - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, - 'nsp', - nsp1_uuid, - name='demo_nsp_1', - network_service_params='{"type": "ip_single", "name": ' - '"vip_ip1", "value": ' - '"self_subnet"}') == 0: - self._log.info( - "\n## Step 2A: Verify NSPolicy demo_nsp_1 with valued " - "attributes, Failed") - return 0 - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, - 'nsp', - nsp2_uuid, - name='demo_nsp_2', - network_service_params='{"type": "ip_single", ' - '"name": "vip_ip2", "value": ' - '"self_subnet"}') == 0: - self._log.info( - "\n## Step 2A: Verify NSPolicy demo_nsp_2 with " - "valued attributes, Failed") - return 0 - # Create two PTGs, each referencing one of the two NSPs - self._log.info( - "\n## Step 3: Create and Verify two PTGs each " - "referencing one of the two NSPs") - uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'group', 'demo_ptg_1', network_service_policy=nsp1_uuid) - if uuid == 0: - self._log.info( - "\n## Step 3A: Create PTG using NSP demo_nsp_1,Failed") - return 0 - else: - ptg1_uuid = uuid[0] - _uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'group', 'demo_ptg_2', network_service_policy=nsp2_uuid) - if _uuid == 0: - self._log.info( - "\n## Step 3B: Create PTG using NSP demo_nsp_2,Failed") - return 0 - else: - ptg2_uuid = _uuid[0] - # Verify - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, 'nsp', nsp1_uuid, policy_target_groups=ptg1_uuid) == 0: - self._log.info( - "\n## Step 3C: Verify PTG demo_ptg_1 seen in NSP " - "demo_nsp_1, Failed") - return 0 - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, 'nsp', nsp2_uuid, policy_target_groups=ptg2_uuid) == 0: - self._log.info( - "\n## Step 3C: Verify PTG demo_ptg_2 seen in NSP " - "demo_nsp_2, Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, 'group', ptg1_uuid, - network_service_policy_id=nsp1_uuid) == 0: - self._log.info( - "\n## Step 3D: Verify PTG demo_ptg_1 references NSP " - "demo_nsp_1, Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, 'group', ptg2_uuid, - network_service_policy_id=nsp2_uuid) == 0: - self._log.info( - "\n## Step 3D: Verify PTG demo_ptg_2 references NSP " - "demo_nsp_2, Failed") - return 0 - # Delete PTGs & NSPs - self._log.info( - "\n## Step 4: Delete and Verify two PTGs each referencing " - "one of the two NSPs") - ptg_list = [ptg1_uuid, ptg2_uuid] - nsp_list = [nsp1_uuid, nsp2_uuid] - for i in range(len(ptg_list)): - if self.gbpcfg.gbp_policy_cfg_all(0, 'group', ptg_list[i]) == 0: - self._log.info( - "\n## Step 4A: Deletion of PTG %s, Failed" % - (ptg_list[i])) - return 0 - if self.gbpcfg.gbp_policy_cfg_all(0, 'nsp', nsp_list[i]) == 0: - self._log.info( - "\n## Step 4B: Deletion of NSP %s, Failed" % - (nsp_list[i])) - return 0 - # Verify - for n in range(len(nsp_list)): - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, 'nsp', nsp_list[n]) != 0: - self._log.info("\n## Step 4C: Verify deletion of NSP, Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_NSP_FUNC_1: PASSED") - return 1 - - def test_gbp_nsp_func_2(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_NSP_FUNC_2: TO CREATE/UPDATE/DELETE/VERIFY a PTG " - "with NTK-SVC-POLICY with MULTIPLE PTGs\n" - "TEST_STEPS::\n" - "Create two NSPolicy Object with non-default params\n" - "Create PTG using one of the two NSPs\n" - "Verify the PTG and NSP are reflecting in each other in the DB\n" - "Update the PTG to use the second NSP\n" - "Verify the PTG and NSP are reflecting in each other in the DB\n" - "Update/Revert the PTG so that it refers to the initial NSP\n" - "Delete all PTG, NSP\n" - "Verify that PTG and NSPs got deleted\n" - "##############################################################\n") - - # Testcase work-flow starts - # Create NSPolicy with non-default attrs - self._log.info('\n## Step 1: Create two NSPolicy ##\n') - nsp1_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'nsp', - 'demo_nsp_1', - network_service_params="type=ip_single,name=vip_ip1," - "value=self_subnet") - if nsp1_uuid == 0: - self._log.info( - "\n## Step 1A: Create NSPolicy with type=ip_single & " - "name:self_subnet == Failed") - return 0 - nsp2_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'nsp', - 'demo_nsp_2', - network_service_params="type=ip_single,name=vip_ip2," - "value=self_subnet") - if nsp2_uuid == 0: - self._log.info( - "\n## Step 1B: Create NSPolicy with type=ip_single & " - "name:self_subnet == Failed") - return 0 - # Create PTG, referencing one of the two NSPs - self._log.info( - "\n## Step 3: Create and Verify PTG referencing one of " - "the two NSPs") - uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'group', 'demo_ptg_1', network_service_policy=nsp1_uuid) - if uuid == 0: - self._log.info( - "\n## Step 3A: Create PTG using NSP demo_nsp_1,Failed") - return 0 - else: - ptg1_uuid = uuid[0] - # Verify - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, 'nsp', nsp1_uuid, policy_target_groups=ptg1_uuid) == 0: - self._log.info( - "\n## Step 3B: Verify PTG demo_ptg_1 seen in NSP " - "demo_nsp_1, Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, 'group', ptg1_uuid, - network_service_policy_id=nsp1_uuid) == 0: - self._log.info( - "\n## Step 3C: Verify PTG demo_ptg_1 references " - "NSP demo_nsp_1, Failed") - return 0 - self._log.info( - "\n## Step 4: Update and Verify the PTG with the second NSP") - # Update the PTG with second NSP and Verify - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'group', ptg1_uuid, network_service_policy=nsp2_uuid) == 0: - self._log.info( - "\n## Step 4A: Updating NSP attribute of PTG, Failed") - return 0 - # Verify - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, 'nsp', nsp1_uuid, policy_target_groups=ptg1_uuid) != 0: - self._log.info( - "\n## Step 4B: Verify PTG demo_ptg_1 is NOT seen " - "in NSP demo_nsp_1, Failed") - return 0 - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, 'nsp', nsp2_uuid, policy_target_groups=ptg1_uuid) == 0: - self._log.info( - "\n## Step 4C: Verify PTG demo_ptg_1 is seen in NSP " - "demo_nsp_2, Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, 'group', ptg1_uuid, - network_service_policy_id=nsp2_uuid) == 0: - self._log.info( - "\n## Step 4D: Verify PTG demo_ptg_1 references NSP " - "demo_nsp_2, Failed") - return 0 - self._log.info( - "\n## Step 5: Update/Revert the NSP attr of PTG and Verify") - # Update the PTG by reverting the NSP to its initial one - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'group', ptg1_uuid, network_service_policy=nsp1_uuid) == 0: - self._log.info( - "\n## Step 5A: Reverting the NSP attribute of PTG by " - "update action, Failed") - return 0 - # Verify - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, 'nsp', nsp2_uuid, policy_target_groups=ptg1_uuid) != 0: - self._log.info( - "\n## Step 5B: Verify PTG demo_ptg_1 is NOT seen in NSP " - "demo_nsp_2, Failed") - return 0 - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, 'nsp', nsp1_uuid, policy_target_groups=ptg1_uuid) == 0: - self._log.info( - "\n## Step 5C: Verify PTG demo_ptg_1 is seen in NSP " - "demo_nsp_1, Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, 'group', ptg1_uuid, - network_service_policy_id=nsp1_uuid) == 0: - self._log.info( - "\n## Step 5D: Verify PTG demo_ptg_1 references NSP " - "demo_nsp_1, Failed") - return 0 - self._log.info( - "\n## Step 6: Delete and Verify two PTGs each referencing " - "one of the two NSPs") - # Delete PTG & NSP - if self.gbpcfg.gbp_policy_cfg_all(0, 'group', ptg1_uuid) == 0: - self._log.info("\n## Step 6A: Deletion of PTG,Failed") - return 0 - nsp_list = [nsp1_uuid, nsp2_uuid] - for i in range(len(nsp_list)): - if self.gbpcfg.gbp_policy_cfg_all(0, 'nsp', nsp_list[i]) == 0: - self._log.info( - "\n## Step 6B: Deletion of NSP %s, Failed" % - (nsp_list[i])) - return 0 - # Verify - for n in range(len(nsp_list)): - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, 'nsp', nsp_list[n]) != 0: - self._log.info("\n## Step 6C: Verify deletion of NSP, Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_NSP_FUNC_2: PASSED") - return 1 - - def test_gbp_nsp_func_3(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_NSP_FUNC_3: TO CREATE/DELETE/VERIFY " - "NTK-SVC-POLICY while REFERENCED IN PTG\n" - "TEST_STEPS::\n" - "Create NSPolicy Object with non-default params\n" - "Create PTG referencing the NSP\n" - "Verify the PTG and NSP are reflecting in each other in the DB\n" - "Delete and Verify the deletion of referenced NSP fails\n" - "Delete PTG & NSP, Verify that PTG and NSPs got deleted\n" - "##############################################################\n") - - # Testcase work-flow starts - # Create NSPolicy with non-default attrs - self._log.info( - '\n## Step 1: Create NSPolicy with non-default params ##\n') - nsp1_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'nsp', - 'demo_nsp_1', - network_service_params="type=ip_single,name=vip_ip1," - "value=self_subnet") - if nsp1_uuid == 0: - self._log.info( - "\n## Step 1A: Create NSPolicy with type=ip_single & " - "name:self_subnet == Failed") - return 0 - # Create PTG, referencing one of the two NSPs - self._log.info( - "\n## Step 2: Create and Verify PTG referencing the NSP") - uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'group', 'demo_ptg_1', network_service_policy=nsp1_uuid) - if uuid == 0: - self._log.info( - "\n## Step 2A: Create PTG using NSP demo_nsp_1,Failed") - return 0 - else: - ptg1_uuid = uuid[0] - # Verify - if self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, 'nsp', nsp1_uuid, policy_target_groups=ptg1_uuid) == 0: - self._log.info( - "\n## Step 2B: Verify PTG demo_ptg_1 seen in NSP demo_nsp_1, " - "Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, 'group', ptg1_uuid, - network_service_policy_id=nsp1_uuid) == 0: - self._log.info( - "\n## Step 2C: Verify PTG demo_ptg_1 references " - "NSP demo_nsp_1, Failed") - return 0 - # Delete the referenced NSP - self._log.info( - "\n## Step 3: Delete the NSP while it is still referenced " - "in a PTG") - if self.gbpcfg.gbp_policy_cfg_all(0, 'nsp', nsp1_uuid) != 0: - self._log.info( - "\n## Step 3A: Deletion of Referenced NSP DID NOT fail") - return 0 - # Delete PTG & NSP - self._log.info("\n## Step 4: Delete PTG followed by NSP and Verify") - if self.gbpcfg.gbp_policy_cfg_all(0, 'group', ptg1_uuid) == 0: - self._log.info("\n## Step 4A: Deletion of PTG,Failed") - return 0 - if self.gbpcfg.gbp_policy_cfg_all(0, 'nsp', nsp1_uuid) == 0: - self._log.info("\n## Step 4B: Deletion of NSP,Failed") - return 0 - # Verify - if self.gbpverify.gbp_l2l3ntk_pol_ver_all(1, 'nsp', nsp1_uuid) != 0: - self._log.info("\n## Step 4C: Verify deletion of NSP, Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_NSP_FUNC_3: PASSED") - return 1 - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pa_func.py b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pa_func.py deleted file mode 100755 index 827594879..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pa_func.py +++ /dev/null @@ -1,253 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import os -import re -import subprocess -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs - - -def main(): - - # Run the Testcase: - test = test_gbp_pa_func() - test.test_cr_ver_del_ver_default() - test.test_upd_ver_del() - utils_libs.report_results('test_gbp_pa_func', 'test_results.txt') - sys.exit(1) - - -class test_gbp_pa_func(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_pa_func.log' - subprocess.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_pa_func.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self._log.info( - "\n## START OF GBP POLICY_ACTION FUNCTIONALITY TESTSUITE\n") - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.act_name = 'demo_act' - - def cleanup(self, cfgobj, uuid_name, tc_name=''): - if tc_name != '': - self._log.info('%s FAILED' % (tc_name)) - if isinstance(cfgobj, str): - cfgobj = [cfgobj] - if isinstance(uuid_name, str): - uuid_name = [uuid_name] - for obj, _id in zip(cfgobj, uuid_name): - if self.gbpcfg.gbp_policy_cfg_all(0, obj, _id): - self._log.info( - 'Success in Clean-up/Delete of Policy Object %s\n' % - (obj)) - else: - self._log.info( - 'Failed to Clean-up/Delete of Policy Object %s\n' % - (obj)) - os._exit(1) - - def test_cr_ver_del_ver_default(self, rep_cr=0, rep_del=0): - - if rep_cr == 0 and rep_del == 0: - self._log.info( - "\n###################################################\n" - "TESTCASE_GBP_PA_FUNC_1: CREATE/VERIFY/DELETE/VERIFY a " - "POLICY ACTION with DEFAULT ATTR VALUE\n" - "TEST_STEPS:\n" - "Create Policy Action Object,default params\n" - "Verify the attributes & value, show & list cmds\n" - "Delete Policy Action using Name\n" - "Verify that PA has got deleted, show & list cmds\n" - "Recreate Policy Action Object inorder to test Delete " - "using UUID\n" - "Delete using UUID\n" - "Verify that PA has got deleted, show & list cmds\n" - "###################################################\n") - - # Testcase work-flow starts - self._log.info( - '\n## Step 1: Create Action with default attrib values##\n') - act_uuid = self.gbpcfg.gbp_action_config(1, self.act_name) - if act_uuid == 0: - self._log.info("# Step 1: Create Action == Failed") - return 0 - if self.gbpverify.gbp_action_verify(0, self.act_name, act_uuid) == 0: - self._log.info( - "# Step 2A: Verify Action using -list option == Failed") - return 0 - if self.gbpverify.gbp_action_verify( - 1, - self.act_name, - id=act_uuid, - action_type='allow', - shared='False') == 0: - self._log.info( - "# Step 2B: Verify Action using -show option == Failed") - return 0 - ###### - self._log.info('\n## Step 3: Delete Action using name ##\n') - if self.gbpcfg.gbp_action_config(0, self.act_name) == 0: - self._log.info("# Step 3: Delete Action using Name == Failed") - return 0 - if self.gbpverify.gbp_action_verify(0, self.act_name, act_uuid) != 0: - self._log.info( - "\n## Step 3A: Verify Action is Deleted using -list option " - "== Failed") - return 0 - if self.gbpverify.gbp_action_verify( - 1, - self.act_name, - id=act_uuid, - action_type='allow', - shared='False') != 0: - self._log.info( - "\n## Step 3B: Verify Action is Deleted using -show option " - "== Failed") - return 0 - - act_uuid = self.gbpcfg.gbp_action_config(1, self.act_name) - if act_uuid: - self._log.info( - "Step 4: Re-created a Policy Action with default inorder " - "to delete with ID") - self._log.info('\n## Step 5: Delete Action using UUID ##\n') - if self.gbpcfg.gbp_action_config(0, act_uuid) == 0: - self._log.info( - "\n## Step 5: Delete Action using UUID == Failed") - return 0 - if self.gbpverify.gbp_action_verify( - 0, act_uuid, self.act_name) != 0: - self._log.info( - "\n## Step 5A: Verify Action is Deleted using -list " - "option == Failed") - return 0 - if self.gbpverify.gbp_action_verify( - 1, - act_uuid, - name=self.act_name, - action_type='allow', - shared='False') != 0: - self._log.info( - "\n## Step 5B: Verify Action is Deleted using -show " - "option == Failed") - return 0 - self._log.info( - "\n## Step 5: Delete of Policy Action using UUID == Passed") - else: - self._log.info( - "\n## Step 6: Recreate of Policy Action using Default " - "== Failed") - return 0 - if rep_cr == 0 and rep_del == 0: - self._log.info("\n## TESTCASE_GBP_PA_FUNC_1: PASSED") - return 1 - - def test_upd_ver_del(self): - self._log.info( - "\n###################################################\n" - "TESTCASE_GBP_PA_FUNC_2: UPDATE/VERIFY/DELETE EDITABLE ATTRIBs " - "of a POLICY ACTION \n" - "TEST_STEPS::\n" - "Create Policy Action using Default param values\n" - "Update the Polciy Action's editable params\n" - "Verify the Policy Action's attributes & values, show & list " - "cmds\n" - "Delete the Policy Action\n" - "Verify Policy Action successfully deleted\n" - "###################################################\n") - - # Testcase work-flow starts - self._log.info( - '\n##Step 1: Create Action with default attrib vals ##\n') - act_uuid = self.gbpcfg.gbp_action_config(1, self.act_name) - if act_uuid == 0: - self._log.info("## Step 1: Create Action == Failed") - return 0 - self._log.info( - "\n## Step 1A: Creating a Service Chain Spec to be used for " - "UPdating Polic Action") - spec_cr_cmd = ('gbp servicechain-spec-create demo_spec | grep id | ' - 'head -1') - cmd_out = subprocess.getoutput(spec_cr_cmd) - spec_id = re.search("\\bid\\b\s+\| (.*) \|", cmd_out, re.I).group(1) - self._log.info( - '\n##Step 2: Update Policy Action Attributes name and ' - 'action_value##\n') - if self.gbpcfg.gbp_action_config( - 2, - act_uuid, - name='grppol_act', - action_value=spec_id) == 0: - self._log.info( - "\n##Step 2: Updating Policy Action's Attributes name " - "& action_value, Failed") - return 0 - - if self.gbpverify.gbp_action_verify( - 0, 'grppol_act', act_uuid, spec_id) == 0: - self._log.info( - "\n## Step 2A: Verify Policy Action Updated Attributes " - "using -list option == Failed") - return 0 - if self.gbpverify.gbp_action_verify( - 1, - 'grppol_act', - id=act_uuid, - action_type='allow', - shared='False', - action_value=spec_id) == 0: - self._log.info( - "\n## Step 2B: Verify Policy Action Updated Attributes " - "using -show option == Failed") - return 0 - if self.gbpcfg.gbp_action_config(0, act_uuid) == 0: - self._log.info("## Step 3: Delete Action using Name == Failed") - return 0 - self._log.info("\n## Step 3A: Now delete the service chain spec") - spec_del_cmd = 'gbp servicechain-spec-delete %s' % (spec_id) - cmd_out = subprocess.getoutput(spec_del_cmd) - if self.gbpverify.gbp_action_verify( - 1, - 'grppol_act', - id=act_uuid, - action_type='allow', - shared='False') != 0: - self._log.info( - "\n## Step 3B: Verify Action is Deleted using -show " - "option == Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_PA_FUNC_2: PASSED") - return 1 - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pa_neg.py b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pa_neg.py deleted file mode 100755 index 268764d0c..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pa_neg.py +++ /dev/null @@ -1,239 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import subprocess -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs - - -def main(): - - # Run the Testcase: - test = test_gbp_pa_neg() - if test.test_pa_invalid_act_type() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PA_NEG_1') - if test.test_pa_valid_type_inval_val() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PA_NEG_2') - if test.test_pa_invalid_act_value() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PA_NEG_3') - if test.test_pa_update_act_type() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PA_NEG_4') - if test.test_pa_update_invalid_act_val() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PA_NEG_5') - if test.test_pa_delete_invalid_pa() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PA_NEG_6') - test.cleanup() - utils_libs.report_results('test_gbp_pa_neg', 'test_results.txt') - sys.exit(1) - - -class test_gbp_pa_neg(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_pa_neg.log' - subprocess.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_pa_neg.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self._log.info("\n## START OF GBP POLICY_ACTION NEGATIVE TESTSUITE\n") - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.act_name = 'demo_act' - - def cleanup(self, tc_name=''): - if tc_name != '': - self._log.info('%s: FAILED' % (tc_name)) - for obj in ['ruleset', 'rule', 'classifier', 'action']: - self.gbpcfg.gbp_del_all_anyobj(obj) - - def test_pa_invalid_act_type(self): - - self._log.info( - "\n###################################################\n" - "TESTCASE_GBP_PA_NEG_1: INVALID Action TYPE\n" - "TEST_STEPS::\n" - "Create and Verify Policy Action Errors Out for Invalid " - "Action Type as Attr\n" - "Invalid Action Type: Null string and string " - "!= 'allow'/'redirect'\n" - "###################################################\n") - for _type in ["", "INVALID"]: - if self.gbpcfg.gbp_action_config( - 1, self.act_name, action_type=_type) != 0: - self._log.info( - "\n## Step 1: Create Action with invalid Action Type=%s " - "did NOT Fail" % - (_type)) - return 0 - if self.gbpverify.gbp_action_verify(1, self.act_name) != 0: - self._log.info( - "\n## Step 1A: Rollback of invalid Action create Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_PA_NEG_1: PASSED") - - def test_pa_valid_type_inval_val(self): - - self._log.info( - "\n###################################################\n" - "TESTCASE_GBP_PA_NEG_2: INVALID Action VALUE for " - "VALID act_type \n" - "TEST_STEPS::\n" - "Create and Verify Policy Action Errors Out for Invalid " - "Action Value for type=REDIRECT\n" - "###################################################\n") - if self.gbpcfg.gbp_action_config( - 1, - self.act_name, - action_type='redirect', - action_value='INVALID') != 0: - self._log.info( - "\n## Step 1: Create Action with invalid Action Value " - "did NOT Fail") - return 0 - if self.gbpverify.gbp_action_verify(1, self.act_name) != 0: - self._log.info( - "\n## Step 1A: Rollback of Invalid Action create Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_PA_NEG_2: PASSED") - - def test_pa_invalid_act_value(self): - - self._log.info( - "\n###################################################\n" - "TESTCASE_GBP_PA_NEG_3: INVALID Action VALUE \n" - "TEST_STEPS::\n" - "Create and Verify Policy Action Errors Out for Invalid " - "Action Value\n" - "###################################################\n") - if self.gbpcfg.gbp_action_config( - 1, self.act_name, action_value='INVALID') != 0: - self._log.info( - "\n## Step 1: Create Action with invalid Action Value " - "did NOT Fail") - return 0 - if self.gbpverify.gbp_action_verify(1, self.act_name) != 0: - self._log.info( - "\n## Step 1A: Rollback of Invalid Action create Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_PA_NEG_3: PASSED") - - def test_pa_update_act_type(self): - - self._log.info( - "\n###################################################\n" - "TESTCASE_GBP_PA_NEG_4: UPDATE Immutable ATTR act_type\n" - "TEST_STEPS::\n" - "Create a valid Policy Action with default action type\n" - "Update name and act_type and Verify that it has failed to " - "update the Policy Action\n" - "###################################################\n") - act_uuid = self.gbpcfg.gbp_action_config(1, self.act_name) - if act_uuid == 0: - self._log.info("## Step 1: Create Action == Failed") - return 0 - if self.gbpcfg.gbp_action_config( - 2, - act_uuid, - name='grppol_act', - action_type='redirect') != 0: - self._log.info( - "\n##Step 2: Updating Policy Action's Attrs name & " - "action_type did NOT Fail") - return 0 - if self.gbpverify.gbp_action_verify( - 1, - self.act_name, - id=act_uuid, - action_type='allow', - shared='False') == 0: - self._log.info( - "\n## Step 2B: Verify Policy Action Attrs are NOT " - "updated == Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_PA_NEG_4: PASSED") - - def test_pa_update_invalid_act_val(self): - - self._log.info( - "\n###################################################\n" - "TESTCASE_GBP_PA_NEG_5: UPDATE act_value with Invalid " - "value for act-type=redirect \n" - "TEST_STEPS::\n" - "Create a valid Policy Action with 'redirect' action type\n" - "Update the attributes act_val='invalid value' and name\n" - "Verify that the update failed and all attrs are having " - "their original vals\n" - "###################################################\n") - - act_uuid = self.gbpcfg.gbp_action_config( - 1, 'new_act', action_type='redirect') - if act_uuid == 0: - self._log.info("## Step 1: Create Action == Failed") - return 0 - if self.gbpcfg.gbp_action_config( - 2, - act_uuid, - name='grppol_act', - action_value='INVALID') != 0: - self._log.info( - "\n##Step 2: Updating Policy Action's Attrs name & " - "action_type did NOT Fail") - return 0 - if self.gbpverify.gbp_action_verify( - 1, - 'new_act', - id=act_uuid, - action_type='redirect', - shared='False') == 0: - self._log.info( - "\n## Step 2B: Verify Policy Action Attrs are NOT " - "updated == Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_PA_NEG_5: PASSED") - - def test_pa_delete_invalid_pa(self): - """ - Delete non-existent Policy Action - """ - self._log.info( - "\n###################################################\n" - "TESTCASE_GBP_PA_NEG_6: DELETE NON-EXISTENT/INVALID " - "POLICY ACTION\n" - "TEST_STEPS::\n" - "Delete non-existent Policy Action\n" - "###################################################\n") - if self.gbpcfg.gbp_action_config(0, 'grppol_act') != 0: - self._log.info( - "\n## Step 1: Expected Error during deletion on " - "non-existent Policy Action == Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_PA_NEG_6: PASSED") - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pc_func.py b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pc_func.py deleted file mode 100755 index 37302e69c..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pc_func.py +++ /dev/null @@ -1,439 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import os -import subprocess -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs - - -def main(): - - # Run the Testcase: - test = test_gbp_pc_func() - if test.test_gbp_pc_func_1() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PC_FUNC_1') - if test.test_gbp_pc_func_2() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PC_FUNC_2') - if test.test_gbp_pc_func_3() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PC_FUNC_3') - if test.test_gbp_pc_func_4() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PC_FUNC_4') - test.cleanup() - utils_libs.report_results('test_gbp_pc_func', 'test_results.txt') - sys.exit(1) - - -class test_gbp_pc_func(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_pc_func.log' - subprocess.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_pc_func.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self._log.info( - "\n## START OF GBP POLICY_CLASSIFIER FUNCTIONALITY TESTSUITE\n") - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.cls_name = 'demo_pc' - self.act_name = 'demo_pa' - - def cleanup(self, tc_name=''): - if tc_name != '': - self._log.info('%s: FAILED' % (tc_name)) - for obj in ['rule', 'classifier', 'action']: - self.gbpcfg.gbp_del_all_anyobj(obj) - - def test_gbp_pc_func_1(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PC_FUNC_1: CREATE/VERIFY/DELETE/VERIFY a POLICY " - "CLASSIFIER with DEFAULT ATTRIB VALUE\n" - "TEST_STEPS::\n" - "Create Policy Classifier Object,default params\n" - "Verify the attributes & value, show & list cmds\n" - "Delete Policy Classifier using Name\n" - "Verify that PC has got deleted, show & list cmds\n" - "Recreate Policy Classifier Object inorder to test Delete " - "using UUID\n" - "Delete using UUID\n" - "Verify that PC has got deleted, show & list cmds\n" - "##############################################################\n") - # Testcase work-flow starts - self._log.info( - "\n## Step 1: Create Classifier with default attrib vals##\n") - cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', self.cls_name) - if cls_uuid != 0: - self._log.info( - "\n## Step 1: Create Classifier Passed, UUID == %s\n" % - (cls_uuid)) - else: - self._log.info("\n## Step 1: Create Classifier == Failed") - return 0 - self._log.info("\n## Step 4A: Verify Classifier using -list cmd") - if self.gbpverify.gbp_classif_verify(0, self.cls_name, cls_uuid) == 0: - self._log.info( - "\n## Step 4A: Verify Classifier using -list option == Failed") - return 0 - self._log.info("\n## Step 4B: Verify Classifier using -show cmd") - if self.gbpverify.gbp_classif_verify( - 1, self.cls_name, id=cls_uuid) == 0: - self._log.info( - "\n## Step 4B: Verify Classifier using -show option == Failed") - return 0 - - ###### - self._log.info("\n## Step 3: Delete Classifier using name ##") - if self.gbpcfg.gbp_policy_cfg_all(0, 'classifier', self.cls_name): - self._log.info( - "\n## Step 3: Delete Classifier using Name == %s, Passed" % - (self.cls_name)) - else: - self._log.info( - "\n## Step 3: Delete Classifier using Name == Failed") - return 0 - if self.gbpverify.gbp_classif_verify(0, self.cls_name, cls_uuid) != 0: - self._log.info( - "\n## Step 3A: Verify Classifier is Deleted using -list " - "option == Failed") - return 0 - if self.gbpverify.gbp_classif_verify( - 1, self.cls_name, id=cls_uuid, shared='False') != 0: - self._log.info( - "\n## Step 3B: Verify Classifier is Deleted using -show " - "option == Failed") - return 0 - - cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', self.cls_name) - if cls_uuid: - self._log.info( - "\n## Step 4: Re-created a Policy Classifier with default " - "inorder to delete with ID") - self._log.info("\n## Step 5: Delete Classifier using UUID ##") - if self.gbpcfg.gbp_policy_cfg_all(0, 'classifier', cls_uuid): - self._log.info( - "\n## Step 5: Delete Classifier Passed using UUID == %s" % - (cls_uuid)) - else: - self._log.info( - "\n## Step 5: Delete Classifier using UUID == Failed") - if self.gbpverify.gbp_classif_verify( - 0, self.cls_name, cls_uuid) != 0: - self._log.info( - "\n## Step 5A: Verify Classifier is Deleted using " - "-list option == Failed") - return 0 - if self.gbpverify.gbp_classif_verify( - 1, self.cls_name, id=cls_uuid, shared='False') != 0: - self._log.info( - "\n## Step 5B: Verify Classifier is Deleted using " - "-show option == Failed") - return 0 - self._log.info( - "\n## Step 5: Delete of Policy Classifier using " - "UUID == Passed") - else: - self._log.info( - "\n## Step 6: Recreate of Policy Classifier using " - "Default == Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_PC_FUNC_1: PASSED") - - def test_gbp_pc_func_2(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PC_FUNC_2: UPDATE/VERIFY/DELETE/VERIFY EACH " - "ATTRIB of a POLICY CLASSIFIER\n" - "TEST_STEP::\n" - "Create Policy Classifier using Default param values\n" - "Update Each the Polciy Classifier's editable params one " - "at a time\n" - "Verify the Policy Classifier's attributes & values, show " - "& list cmds\n" - "Delete the Policy Classifier\n" - "Verify Policy Classifier successfully deleted\n" - "##############################################################\n") - # Testcase work-flow starts - self._log.info( - "\n## Step 1: Create Classifier with default attrib vals##\n") - cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', self.cls_name) - if cls_uuid != 0: - self._log.info( - "\n## Step 1: Create Classifier Passed, UUID == %s\n" % - (cls_uuid)) - else: - self._log.info("\n## Step 1: Create Classifier == Failed") - return 0 - # for attr,val in attrib.iteritems(): - self._log.info( - "\n## Step 2: Update Policy Classifier attributes one at a " - "time %s ##") - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'classifier', cls_uuid, name='grppol_pc') == 0: - self._log.info( - "\n## Step 2: Updating Policy Classifier's attribute " - "Name, Failed") - return 0 - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'classifier', cls_uuid, protocol='tcp') == 0: - self._log.info( - "\n## Step 2: Updating Policy Classifier's attribute " - "Protocol, Failed") - return 0 - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'classifier', cls_uuid, direction='bi') == 0: - self._log.info( - "\n## Step 2: Updating Policy Classifier's attribute " - "Direction, Failed") - return 0 - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'classifier', cls_uuid, port_range='22:1022') == 0: - self._log.info( - "\n## Step 2: Updating Policy Classifier's attribute " - "Port Range, Failed") - return 0 - if self.gbpverify.gbp_classif_verify( - 0, 'grppol_pc', cls_uuid, 'tcp', 'bi', '22:1022') == 0: - self._log.info( - "\n## Step 2A: Verify Policy Classifier Updated Attributes " - "using -list option == Failed") - return 0 - if self.gbpverify.gbp_classif_verify( - 1, - 'grppol_pc', - id=cls_uuid, - protocol='tcp', - port_range='22:1022', - direction='bi') == 0: - self._log.info( - "\n## Step 2B: Verify Policy Classifier Updated Attributes " - "using -show option == Failed") - return 0 - if self.gbpcfg.gbp_policy_cfg_all(0, 'classifier', cls_uuid): - self._log.info( - "\n## Step 3: Deleted the Classifier == %s\n" % - (cls_uuid)) - else: - self._log.info( - "\n## Step 3: Delete Classifier using Name == Failed") - return 0 - if self.gbpverify.gbp_classif_verify(1, 'grppol_pc', id=cls_uuid) != 0: - self._log.info( - "\n## Step 3B: Verify Classifier is Deleted using " - "-show option == Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_PC_FUNC_2: PASSED") - - def test_gbp_pc_func_3(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PC_FUNC_3: UPDATE/VERIFY/DELETE/VERIFY ALL " - "ATTRIB of a POLICY CLASSIFIER @ ONCE \n" - "TEST_STEP::\n" - "Create Policy Classifier using Default param values\n" - "Update All the Polciy Classifier's editable params at one shot\n" - "Verify the Policy Classifier's attributes & values, show " - "& list cmds\n" - "Delete the Policy Classifier\n" - "Verify Policy Classifier successfully deleted\n" - "##############################################################\n") - # Testcase work-flow starts - self._log.info( - "\n## Step 1: Create Classifier with default attrib vals ##\n") - cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', self.cls_name) - if cls_uuid != 0: - self._log.info( - "\n## Step 1: Create Classifier Passed, UUID == %s\n" % - (cls_uuid)) - else: - self._log.info("\n## Step 1: Create Classifier == Failed") - return 0 - self._log.info( - "\n###########################################\n" - "## Step 2: Update Policy Classifier Attributes ##\n" - "## protocol, port-range,name,direction,description ##\n" - "#################################################\n") - if self.gbpcfg.gbp_policy_cfg_all( - 2, - 'classifier', - cls_uuid, - name='grppol_pc', - protocol='tcp', - direction='bi', - port_range='22:1022', - description="'For devstack demo'"): - self._log.info( - "\n## Step 2: Updating Policy Classifier's Attributes " - "name,protocol,port-range,name,direction,description, Passed") - else: - self._log.info( - "\n## Step 2: Updating Policy Classifier's Attributes " - "name,protocol,port-range,name,direction,description, Failed") - return 0 - - if self.gbpverify.gbp_classif_verify( - 0, 'grppol_pc', cls_uuid, 'tcp', '22:1022', 'bi') == 0: - self._log.info( - "\n## Step 2A: Verify Policy Classifier Updated " - "Attributes using -list option == Failed") - return 0 - if self.gbpverify.gbp_classif_verify( - 1, - 'grppol_pc', - id=cls_uuid, - protocol='tcp', - direction='bi', - port_range='22:1022', - description='For devstack demo') == 0: - self._log.info( - "\n## Step 2B: Verify Policy Classifier Updated " - "Attributes using -show option == Failed") - return 0 - if self.gbpcfg.gbp_policy_cfg_all(0, 'classifier', 'grppol_pc') == 0: - self._log.info( - "\n## Step 3: Delete Classifier using Name == Failed") - return 0 - if self.gbpverify.gbp_classif_verify(1, 'grppol_pc', id=cls_uuid) != 0: - self._log.info( - "\n## Step 3B: Verify Classifier is Deleted using " - "-show option == Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_PC_FUNC_3: PASSED") - - def test_gbp_pc_func_4(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PC_FUNC_4: CREATE/SHARE/DELETE/ POLICY CLASSIFIER " - "among MULTIPLE POLICY RULES \n" - "TEST_STEP::\n" - "Create and Verify Policy Classifier with valued attributes\n" - "Create multiple(n=10) Policy Rules referencing the same " - "Policy Classifier\n" - "Verify the Policy Classifier is referenced in all " - "configured Policy Rules\n" - "Delete the Policy Classifier, verify it's deletion " - "fails until all Policy Rules are deleted\n" - "Verify Policy Classifier successfully deleted\n" - "##############################################################\n") - # Testcase work-flow starts - self._log.info( - "\n## Step 0: Creating a Policy Action needed for this test\n") - act_uuid = self.gbpcfg.gbp_policy_cfg_all(1, 'action', self.act_name) - if act_uuid == 0: - self._log.info("\n## Testcase_gbp_pc_func_4: ABORTED\n") - os._exit(1) - self._log.info( - "\n## Step 1: Create and Verify Classifier with valued " - "attrib ##\n") - cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'classifier', - self.cls_name, - protocol='tcp', - direction='bi', - port_range='22:1022', - description="'For devstack demo'") - if cls_uuid == 0: - self._log.info("\n## Step 1A: Create Classifier == Failed") - return 0 - if self.gbpverify.gbp_classif_verify( - 1, - self.cls_name, - id=cls_uuid, - protocol='tcp', - direction='bi', - port_range='22:1022', - description='For devstack demo') == 0: - self._log.info( - "\n## Step 1B: Verify Policy Classifier Attributes using " - "-show option == Failed") - return 0 - self._log.info( - "\n## Step 2: Create Multiple Policy Rules, each referencing " - "the same classifier ##\n") - for n in range(1, 11): - if self.gbpcfg.gbp_policy_cfg_all( - 1, 'rule', 'grppol_pr_%s' % - (n), classifier=cls_uuid, action=act_uuid) == 0: - self._log.info( - "\n## Step 2A: Policy Rule grppol_pr_%s creation, Failed" % - (n)) - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'rule', - 'grppol_pr_%s' % - (n), - policy_classifier_id=cls_uuid, - policy_actions=act_uuid) == 0: - self._log.info( - "\n## Step 2B: Policy Rule grppol_pr_%s referencing " - "same classifier, Failed ##\n" % - (n)) - return 0 - self._log.info( - "\n## Step 3: Delete Policy Classifier and Policy Rule and " - "verify deletion fails ##") - for i in range(1, 11): - if self.gbpcfg.gbp_policy_cfg_all(0, 'classifier', cls_uuid) != 0: - self._log.info( - "\n## Step 3A: Referenced Policy Classifier's " - "deletion DID NOT fail ##") - return 0 - if self.gbpcfg.gbp_policy_cfg_all( - 0, 'rule', 'grppol_pr_%s' % - (i)) == 0: - self._log.info( - "\n## Step 3B: Referencing Policy Rule's deletion, " - "Failed ##") - return 0 - self._log.info( - "\n## Step 4: Deletion of Policy Classifier, all referencing " - "Policy Rules have been deleted ##") - if self.gbpcfg.gbp_policy_cfg_all(0, 'classifier', cls_uuid) == 0: - self._log.info( - "\n## Step 4A: Policy Classifier's deletion, Failed ##") - return 0 - if self.gbpverify.gbp_classif_verify(1, 'grppol_pc', id=cls_uuid) != 0: - self._log.info( - "\n## Step 4B: Verify Classifier is Deleted, Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_PC_FUNC_4: PASSED") - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pc_neg.py b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pc_neg.py deleted file mode 100755 index 19a74066d..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pc_neg.py +++ /dev/null @@ -1,267 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import subprocess -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs - - -def main(): - - # Run the Testcase: - test = test_gbp_pc_neg() - if test.test_gbp_pc_neg_1() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PC_NEG_1') - if test.test_gbp_pc_neg_2() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PC_NEG_2') - if test.test_gbp_pc_neg_3() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PC_NEG_3') - if test.test_gbp_pc_neg_4() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PC_NEG_4') - if test.test_gbp_pc_neg_5() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PC_NEG_5') - if test.test_gbp_pc_neg_6() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PC_NEG_6') - test.cleanup() - utils_libs.report_results('test_gbp_pc_neg', 'test_results.txt') - sys.exit(1) - - -class test_gbp_pc_neg(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_pc_neg.log' - subprocess.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_pc_neg.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self._log.info( - "\n## START OF GBP POLICY_CLASSIFIER NEGATIVE TESTSUITE\n") - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.cls_name = 'demo_pc' - - def cleanup(self, tc_name=''): - if tc_name != '': - self._log.info('%s FAILED' % (tc_name)) - for obj in ['classifier']: - self.gbpcfg.gbp_del_all_anyobj(obj) - - def test_gbp_pc_neg_1(self): - - self._log.info( - "\n#######################################################\n" - "TESTCASE_GBP_PC_NEG_1: CREATE/VERIFY a " - "POLICY CLASSIFIER with INVALID PROTO \n" - "TEST_STEP::\n" - "Create Policy Classifier with Invalid " - "Proto(any proto other than tcp,udp,icmp)\n" - "Verify that the create fails and rollbacks\n" - "#######################################################\n") - - # Testcase work-flow starts - self._log.info( - "\n## Step 1: Create Classifier with invalid protocol##\n") - if self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', self.cls_name, protocol='http') != 0: - self._log.info( - "\n## Step 1: Create Classifier with Invalid Protocol " - "did NOT Fail") - return 0 - self._log.info("\n## Step 1A: Verify classifier has been rolled back") - if self.gbpverify.gbp_classif_verify(1, self.cls_name) != 0: - self._log.info("\n## Step 1A: Classifier did NOT roll back") - return 0 - self._log.info("\n## TESTCASE_GBP_PC_NEG_1: PASSED") - return 1 - - def test_gbp_pc_neg_2(self): - - self._log.info( - "\n#######################################################\n" - "TESTCASE_GBP_PC_NEG_2: CREATE/VERIFY a POLICY CLASSIFIER with " - "INVALID PORT-RANGE \n" - "TEST_STEP::\n" - "Create Policy Classifier with Valid Proto BUT Invalid " - "Port-range\n" - "Verify that the create fails and rollbacks\n" - "#######################################################\n") - - # Testcase work-flow starts - self._log.info( - "\n## Step 1: Create Classifier with invalid protocol##\n") - if self.gbpcfg.gbp_policy_cfg_all( - 1, - 'classifier', - self.cls_name, - protocol='tcp', - port_range='80:50') != 0: - self._log.info( - "\n## Step 1: Create Classifier with Invalid Port-Range " - "did NOT Fail") - return 0 - self._log.info("\n## Step 1A: Verify classifier has been rolled back") - if self.gbpverify.gbp_classif_verify(1, self.cls_name) != 0: - self._log.info("\n## Step 1A: Classifier did NOT roll back") - return 0 - self._log.info("\n## TESTCASE_GBP_PC_NEG_2: PASSED") - return 1 - - def test_gbp_pc_neg_3(self): - - self._log.info( - "\n#######################################################\n" - "TESTCASE_GBP_PC_NEG_3: DELETE NON-EXISTENT/INVALID " - "POLICY CLASSIFICER\n" - "TEST_STEP::\n" - "Delete unknown/invalid policy-classifier\n" - "#######################################################\n") - - self._log.info("\n## Step 1: Delete non-existent Classifier ##") - if self.gbpcfg.gbp_policy_cfg_all(0, 'classifier', self.cls_name) != 0: - self._log.info( - "\n## Step 1: Delete Non-existent policy classifier " - "did NOT Fail") - return 0 - self._log.info("\n## TESTCASE_GBP_PC_NEG_3: PASSED") - return 1 - - def test_gbp_pc_neg_4(self): - - self._log.info( - "\n#######################################################\n" - "TESTCASE_GBP_PC_NEG_4: CREATE/VERIFY POLICY CLASSIFIER " - "with INVALID DIRECTION \n" - "TEST_STEP::\n" - "Create Policy Classifier using Invalid Direction\n" - "Verify that the create fails and rollbacks\n" - "######################################################\n") - - # Testcase work-flow starts - self._log.info( - "\n## Step 1: Create Classifier with Invalid Direction##\n") - if self.gbpcfg.gbp_policy_cfg_all( - 1, - 'classifier', - self.cls_name, - direction='redirect') != 0: - self._log.info( - "\n## Step 1: Create Classifier with Invalid Direction " - "did NOT Fail") - return 0 - self._log.info("\n## Step 1A: Verify classifier has been rolled back") - if self.gbpverify.gbp_classif_verify(1, self.cls_name) != 0: - self._log.info("\n## Step 1A: Classifier did NOT roll back") - return 0 - self._log.info("\n## TESTCASE_GBP_PC_NEG_4: PASSED") - return 1 - - def test_gbp_pc_neg_5(self): - - self._log.info( - "\n#######################################################\n" - "TESTCASE_GBP_PC_NEG_5: UPDATE/VERIFY/ POLICY CLASSIFIER " - "with INVALID PROTOCOL \n" - "TEST_STEP::\n" - "Create Policy Classifier using non-default protocol\n" - "Update Policy Classifier with Invalid Protocol\n" - "Verify that the update fails and rollbacks to original values\n" - "######################################################\n") - - # Testcase work-flow starts - self._log.info( - "\n## Step 1: Create Classifier with non-default protocol ##\n") - self.cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', self.cls_name, protocol='tcp') - if self.cls_uuid != 0: - self._log.info( - "\n## Step 1: Create Classifier Passed, UUID == %s\n" % - (self.cls_uuid)) - else: - self._log.info("\n## Step 1: Create Classifier == Failed") - return 0 - self._log.info( - "\n## Step 2: Update Policy Classifier with Invalid Protocol##\n") - if self.gbpcfg.gbp_policy_cfg_all( - 2, - 'classifier', - self.cls_uuid, - name='grppol_pc', - protocol='http') != 0: - self._log.info( - "\n## Step 2: Updating Policy Classifier's with " - "Invalid Protocol did NOT Fail ") - return 0 - if self.gbpverify.gbp_classif_verify( - 1, self.cls_name, id=self.cls_uuid, protocol='tcp') == 0: - self._log.info( - "\n## Step 2A: Verify Policy Classifier did NOT roll back") - return 0 - self._log.info("\n## TESTCASE_GBP_PC_NEG_5: PASSED") - return 1 - - def test_gbp_pc_neg_6(self): - - self._log.info( - "\n######################################################\n" - "TESTCASE_GBP_PC_NEG_6: UPDATE/VERIFY/ POLICY CLASSIFIER " - "with INVALID PORT-RANGE \n" - "TEST_STEP::\n" - "Update Policy Classifier with Invalid Port-Range\n" - "Verify that the update fails and rollbacks to original values\n" - "#####################################################\n") - - # Testcase work-flow starts - self._log.info( - "\n## Step 2: Update Policy Classifier with Invalid " - "Port-Range##\n") - if self.gbpcfg.gbp_policy_cfg_all( - 2, - 'classifier', - self.cls_uuid, - name='grppol_pc', - port_range='4000:80') != 0: - self._log.info( - "\n## Step 2: Updating Policy Classifier's with " - "Invalid Port-Range did NOT Fail ") - return 0 - if self.gbpverify.gbp_classif_verify( - 1, - self.cls_name, - id=self.cls_uuid, - protocol='tcp', - port_range='4000:80') != 0: - self._log.info( - "\n## Step 2A: Verify Policy Classifier did NOT roll back") - return 0 - self._log.info("\n## TESTCASE_GBP_PC_NEG_6: PASSED") - return 1 - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pr_func.py b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pr_func.py deleted file mode 100755 index e8a2e2e31..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pr_func.py +++ /dev/null @@ -1,581 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import os -import subprocess -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs - - -def main(): - - # Run the Testcases: - test = test_gbp_pr_func() - if test.test_gbp_pr_func_1() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PR_FUNC_1') - if test.test_gbp_pr_func_2() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PR_FUNC_2') - if test.test_gbp_pr_func_3() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PR_FUNC_3') - if test.test_gbp_pr_func_4() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PR_FUNC_4') - if test.test_gbp_pr_func_5() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PR_FUNC_5') - if test.test_gbp_pr_func_6() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PR_FUNC_6') - test.cleanup() - utils_libs.report_results('test_gbp_pr_func', 'test_results.txt') - sys.exit(1) - - -class test_gbp_pr_func(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_pr_func.log' - subprocess.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_pr_func.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self._log.info( - "\n## START OF GBP POLICY_RULE FUNCTIONALITY TESTSUITE\n") - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.act_name = 'demo_pa' - self.cls_name = 'demo_pc' - self.rule_name = 'demo_pr' - self._log.info('\n## Step 1: Create a PC needed for PR Testing ##') - self.cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', self.cls_name) - if self.cls_uuid == 0: - self._log.info( - "\nReqd Classifier Create Failed, hence GBP Policy Rule " - "Functional Test Suite Run ABORTED\n") - os._exit(1) - self._log.info('\n## Step 1: Create a PA needed for PR Testing ##') - self.act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', self.act_name) - if self.act_uuid == 0: - self._log.info( - "\nReqd Action Create Failed, hence GBP Policy Rule " - "Functional Test Suite Run ABORTED\n") - os._exit(1) - - def cleanup(self, tc_name=''): - if tc_name != '': - self._log.info('%s: FAILED' % (tc_name)) - for obj in ['rule', 'classifier', 'action']: - self.gbpcfg.gbp_del_all_anyobj(obj) - - def test_gbp_pr_func_1( - self, - name_uuid='', - rule_uuid='', - rep_cr=0, - rep_del=0): - - if rep_cr == 0 and rep_del == 0: - self._log.info( - "\n########################################################\n" - "TESTCASE_GBP_PR_FUNC_1: TO CREATE/VERIFY/DELETE/VERIFY a " - "POLICY RULE with DEFAULT ATTRIB VALUE\n" - "TEST_STEP::\n" - "Create Policy Rule Object,default params(Classifier is a " - "reqd param)\n" - "Verify the attributes & value, show & list cmds\n" - "Delete Policy Rule using Name\n" - "Verify that PR has got deleted, show & list cmds\n" - "##########################################################\n") - - if name_uuid == '': - name_uuid = self.rule_name - # Testcase work-flow starts - if rep_cr == 0 or rep_cr == 1: - self._log.info( - '\n## Step 1: Create Rule with default attrib vals##\n') - rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'rule', name_uuid, classifier=self.cls_name, - action=self.act_uuid) - if rule_uuid == 0: - self._log.info("# Step 1: Create Rule == Failed") - return 0 - self._log.info('# Step 2A: Verify Rule using -list cmd') - if self.gbpverify.gbp_policy_verify_all( - 0, 'rule', name_uuid, rule_uuid, 'True') == 0: - self._log.info( - "# Step 2A: Verify Rule using -list option == Failed") - return 0 - self._log.info('# Step 2B: Verify Rule using -show cmd') - if self.gbpverify.gbp_policy_verify_all( - 1, - 'rule', - name_uuid, - id=rule_uuid, - policy_classifier_id=self.cls_uuid, - enabled='True') == 0: - self._log.info( - "# Step 2B: Verify Rule using -show option == Failed") - return 0 - ####### - if rep_del == 0 or rep_del == 1: - self._log.info('\n## Step 3: Delete Rule using name ##\n') - if self.gbpcfg.gbp_policy_cfg_all(0, 'rule', name_uuid) == 0: - self._log.info("# Step 3: Delete Rule == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 0, 'rule', name_uuid, rule_uuid) != 0: - self._log.info( - "# Step 3A: Verify Rule is Deleted using " - "-list option == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, 'rule', name_uuid, id=rule_uuid, shared='False') != 0: - self._log.info( - "# Step 3B: Verify Rule is Deleted using " - "-show option == Failed") - return 0 - if rep_cr == 0 and rep_del == 0: - self._log.info("\n## TESTCASE_GBP_PR_FUNC_1: PASSED") - return 1 - - def test_gbp_pr_func_2(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PR_FUNC_2: TO CREATE/VERIFY/DELETE/VERIFY a " - "POLICY RULE with POLICY ACTION & CLASSIFIER\n" - "TEST_STEP::\n" - "Create Policy Rule Object with GBP PA & PC\n" - "Verify the attributes & value, show & list cmds\n" - "Delete Policy Rule using Name\n" - "Verify that PR has got deleted, show & list cmds\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info("\n## Step 1: Create Policy Rule with PC & PA ##") - rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'rule', self.rule_name, classifier=self.cls_name, - action=self.act_name) - if rule_uuid == 0: - self._log.info("# Step 1: Create Rule == Failed") - return 0 - self._log.info('# Step 2A: Verify Rule using -list cmd') - if self.gbpverify.gbp_policy_verify_all( - 0, 'rule', self.rule_name, rule_uuid, 'True') == 0: - self._log.info( - "# Step 2A: Verify Rule using -list option == Failed") - return 0 - self._log.info('# Step 2B: Verify Rule using -show cmd') - if self.gbpverify.gbp_policy_verify_all( - 1, - 'rule', - self.rule_name, - id=rule_uuid, - policy_classifier_id=self.cls_uuid, - enabled='True', - policy_actions=self.act_uuid) == 0: - self._log.info( - "# Step 2B: Verify Rule using -show option == Failed") - return 0 - self.test_gbp_pr_func_1(rule_uuid=rule_uuid, rep_cr=2) - self._log.info("\n## TESTCASE_GBP_PR_FUNC_2: PASSED") - return 1 - - def test_gbp_pr_func_3(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PR_FUNC_3: TO UPDATE/VERIFY/DELETE/VERIFY " - "EACH ATTRIB of a POLICY RULE\n" - "TEST_STEP::\n" - "Create Policy Rule using Default param values\n" - "Update Each the Polciy Rule's editable params one at a time\n" - "Verify the Policy Rule's attributes & values, show & list cmds\n" - "Delete the Policy Rule\n" - "Verify Policy Rule successfully deleted\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info('\n## Step 1: Create Policy Rule with PA and PC##\n') - rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'rule', self.rule_name, classifier=self.cls_name, - action=self.act_name) - if rule_uuid != 0: - self._log.info( - "Step 1: Create Rule Passed, UUID == %s\n" % - (rule_uuid)) - else: - self._log.info("# Step 1: Create Rule == Failed") - return 0 - self._log.info('\n## Step 1A: Create new PA and new PC##\n') - new_cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', 'grppol_pc1') - if new_cls_uuid == 0: - self._log.info( - "\nNew Classifier Create Failed, hence " - "Testcase_gbp_pr_func_3 ABORTED\n") - return 0 - new_act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', 'grppol_pa1') - if new_act_uuid == 0: - self._log.info( - "\nNew Action Create Failed, hence " - "Testcase_gbp_pr_func_3 ABORTED\n") - return 0 - - attrib_list = [{'name': 'grppol_pr'}, {'classifier': 'grppol_pc1'}, { - 'action': 'grppol_pa1'}, {'description': "'For devstack demo'"}] - for attr_val in attrib_list: - if self.gbpcfg.gbp_policy_cfg_upd_all( - 'rule', rule_uuid, attr_val) == 0: - self._log.info( - "\nStep 2: Updating Policy Rule's Attribute %s, Failed" % - (attr_val)) - return 0 - # Verify starts - if self.gbpverify.gbp_policy_verify_all( - 0, 'rule', 'grppol_pr', rule_uuid, 'True') == 0: - self._log.info( - "# Step 2A: Verify Policy Rule Updated Attributes " - "using -list option == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'rule', - rule_uuid, - name='grppol_pr', - policy_classifier_id=new_cls_uuid, - policy_actions=new_act_uuid, - description='For devstack demo') == 0: - self._log.info( - "# Step 2B: Verify Policy Rule Updated Attributes " - "using -show option == Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_PR_FUNC_3: PASSED") - return 1 - - def test_gbp_pr_func_4(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PR_FUNC_4: TO UPDATE/VERIFY/DELETE/VERIFY ALL " - "ATTRIB of a POLICY RULE @ ONCE \n" - "TEST_STEP::\n" - "Create Policy Rule using Default param values\n" - "Update All the Policy Rule's editable params at one shot\n" - "Verify the Policy Rule's attributes & values, show & list cmds\n" - "Delete the Policy Rule\n" - "Verify Policy Rule successfully deleted\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info('\n## Step 1: Create Policy Rule with PA and PC##\n') - rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'rule', self.rule_name, classifier=self.cls_name, - action=self.act_name) - if rule_uuid != 0: - self._log.info( - "Step 1: Create Rule Passed, UUID == %s\n" % - (rule_uuid)) - else: - self._log.info("# Step 1: Create Rule == Failed") - return 0 - self._log.info('\n## Step 1A: Create new PA and new PC##\n') - new_cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', 'grppol_pc2') - if new_cls_uuid == 0: - self._log.info( - "\nNew Classifier Create Failed, hence " - "Testcase_gbp_pr_func_4 ABORTED\n") - os._exit(1) - new_act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', 'grppol_pa2') - if new_act_uuid == 0: - self._log.info( - "\nNew Action Create Failed, hence " - "Testcase_gbp_pr_func_4 ABORTED\n") - os._exit(1) - self._log.info( - '\n###########################################\n' - '## Step 2: Update Policy Rule Attributes ##\n' - '## protocol, port-range,name,direction,description ##\n' - '#################################################\n') - if self.gbpcfg.gbp_policy_cfg_all( - 2, - 'rule', - rule_uuid, - name='grppol_pr', - classifier=new_cls_uuid, - action=new_act_uuid, - description="'For devstack demo'"): - self._log.info( - "\nStep 2: Updating Policy Rule's Attributes " - "name,protocol,port-range,name,direction,description, Passed") - else: - self._log.info( - "\nStep 2: Updating Policy Rule's Attributes " - "name,protocol,port-range,name,direction,description, Failed") - return 0 - # Verify starts - if self.gbpverify.gbp_policy_verify_all( - 0, 'rule', 'grppol_pr', rule_uuid, 'True') == 0: - self._log.info( - "# Step 2A: Verify Policy Rule Updated " - "Attributes using -list option == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'rule', - rule_uuid, - name='grppol_pr', - policy_classifier_id=new_cls_uuid, - policy_actions=new_act_uuid, - description='For devstack demo') == 0: - self._log.info( - "# Step 2B: Verify Policy Rule Updated " - "Attributes using -show option == Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_PR_FUNC_4: PASSED") - return 1 - - def test_gbp_pr_func_5(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PR_FUNC_5: CREATE/SHARE/DELETE/ POLICY " - "RULE among MULTIPLE POLICY RULE-SETs \n" - "TEST_STEP::\n" - "Create and Verify Policy Rule with valued " - "attributes(action & classifer)\n" - "Create multiple(n=10) Policy Rule-Set " - "referencing the same Policy Rule\n" - "Verify the Policy Rule is referenced in " - "all configured Policy Rules\n" - "Delete the Policy Rule, verify it's " - "deletion fails until all Policy Rule-Sets are deleted\n" - "Verify Policy Rule successfully deleted\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info( - '\n## Step 1: Create new PA ,new PC, 1 PR using the same ' - 'PA & PC##\n') - new_cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', 'grppol_pc1') - if new_cls_uuid == 0: - self._log.info( - "\nNew Classifier Create Failed, hence " - "Testcase_gbp_pr_func_5 ABORTED\n") - return 0 - new_act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', 'grppol_pa1') - if new_act_uuid == 0: - self._log.info( - "\nNew Action Create Failed, hence " - "Testcase_gbp_pr_func_5 ABORTED\n") - return 0 - rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'rule', - 'grppol_pr', - classifier=new_cls_uuid, - action=new_act_uuid, - description="'For devstack demo'") - if rule_uuid == 0: - self._log.info("##\n Step 1B: Policy Rule create, failed\n") - return 0 - self._log.info( - "\n## Step 2: Create Multiple PRS referencing the same PR") - for n in range(1, 11): - if self.gbpcfg.gbp_policy_cfg_all( - 1, - 'ruleset', - 'grppol_prs_%s' % - (n), - policy_rule=rule_uuid, - description="'For devstack demo'") == 0: - self._log.info( - "##\n Step 2A: Policy Rule-Set creation " - "referencing same Policy Rule, Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, 'ruleset', 'grppol_prs_%s' % - (n), policy_rules=rule_uuid) == 0: - self._log.info( - "##\n Step 2B: Verify Policy Rule-Set " - "grppol_prs_%s referencing same Policy Rule, Failed" % - (n)) - return 0 - self._log.info( - "\n## Step 3: Delete Policy Rule and Policy " - "Rule-Set and verify deletion fails ##") - for i in range(1, 11): - if self.gbpcfg.gbp_policy_cfg_all(0, 'rule', rule_uuid) != 0: - self._log.info( - "\n## Step 3A: Referenced Policy Rule's " - "deletion DID NOT fail ##") - return 0 - if self.gbpcfg.gbp_policy_cfg_all( - 0, 'ruleset', 'grppol_prs_%s' % - (i)) == 0: - self._log.info( - "\n## Step 3B: Referencing Policy " - "Rule-Set's deletion, Failed ##") - return 0 - self._log.info( - "\n## Step 4: Deletion of Policy Rule, all " - "referencing Policy Rule-Sets has been deleted ##") - if self.gbpcfg.gbp_policy_cfg_all(0, 'rule', rule_uuid) == 0: - self._log.info("\n## Step 4A: Policy " - "Rule's deletion, Failed ##") - return 0 - if self.gbpverify.gbp_action_verify(1, 'grppol_pr', id=rule_uuid) != 0: - self._log.info( - "\n## Step 4B: Verify Policy Rule is Deleted, Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_PR_FUNC_5: PASSED") - return 1 - - def test_gbp_pr_func_6(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PR_FUNC_6: TO UPDATE ALL ATTRIB " - "of a POLICY CLASSIFIER USED IN A POLICY RULE \n" - "TEST_STEP::\n" - "Create Policy Rule using a Policy Action and Policy Classifier\n" - "Update All the in-use Policy Classifier's " - "editable params at one shot\n" - "Verify the Policy Classifier's updated attributes & values\n" - "Delete the Policy Rule\n" - "Rever the Policy Classifier's editable params\n" - "Verify the Policy Classifier's updated attributes " - "& values, show & list cmds\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info('\n## Step 1: Create Policy Rule with PA and PC##\n') - self._log.info('\n## Step 1A: Create new PA and new PC##\n') - new_cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', 'grppol_pc2', protocol='tcp', - port_range='100:300') - if new_cls_uuid == 0: - self._log.info( - "\nNew Classifier Create Failed, hence " - "Testcase_gbp_pr_func_6 ABORTED\n") - os._exit(1) - new_act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', 'grppol_pa2') - if new_act_uuid == 0: - self._log.info( - "\nNew Action Create Failed, hence " - "Testcase_gbp_pr_func_6 ABORTED\n") - os._exit(1) - rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'rule', self.rule_name, classifier=new_cls_uuid, - action=new_act_uuid) - if rule_uuid != 0: - self._log.info( - "Step 1B: Create Rule Passed, UUID == %s\n" % - (rule_uuid)) - else: - self._log.info("# Step 1B: Create Rule == Failed") - return 0 - self._log.info( - '\n###########################################\n' - '## Step 2: Update in-use Policy Classifier editable params ##\n' - '## protocol, port-range,name,direction,description ##\n' - '#################################################\n') - if self.gbpcfg.gbp_policy_cfg_all( - 2, - 'classifier', - new_cls_uuid, - protocol='udp', - direction='bi', - port_range='640:1022', - description="'For devstack demo'") != 0: - self._log.info( - "\nStep 2: Updating in-use Policy Classifier's Attributes " - "protocol,port-range,direction,description, Passed") - else: - self._log.info( - "\nStep 2: Updating in-use Policy Classifier's Attributes " - "protocol,port-range,direction,description, Failed") - return 0 - # Verify starts - self._log.info( - '\n## Step 3: Verify the in-use Policy Classifier updated ' - 'attributes & values\n') - if self.gbpverify.gbp_policy_verify_all( - 1, - 'classifier', - new_cls_uuid, - protocol='udp', - direction='bi', - port_range='640:1022') == 0: - self._log.info( - "# Step 3: Verify Policy CLassifier Updated Attributes " - "using -show option == Failed") - return 0 - # Delete Policy Rule and Re-update the Policy Classifier - self._log.info('\n## Step 4: Delete the Policy Rule\n') - if self.gbpcfg.gbp_policy_cfg_all(0, 'rule', rule_uuid) == 0: - self._log.info("\n## Step 4: Policy Rule's deletion, failed ##") - return 0 - self._log.info( - '\n## Step 5: Update the Policy Classifier editable params\n') - if self.gbpcfg.gbp_policy_cfg_all( - 2, - 'classifier', - new_cls_uuid, - protocol='tcp', - port_range='100:300') == 0: - self._log.info( - "\nStep 5: Updating in-use Policy Classifier Attributes " - "protocol,port_range, Failed") - return 0 - # Verify starts - self._log.info( - '\n## Step 6: Verify the in-use Policy Classifier updated ' - 'attributes & values\n') - if self.gbpverify.gbp_policy_verify_all( - 1, - 'classifier', - new_cls_uuid, - protocol='tcp', - direction='bi', - port_range='100:300') == 0: - self._log.info( - "# Step 6: Verify Policy CLassifier Updated Attributes " - "using -show option == Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_PR_FUNC_6: PASSED") - return 1 - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pr_neg.py b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pr_neg.py deleted file mode 100755 index 42122f4f5..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pr_neg.py +++ /dev/null @@ -1,222 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import os -import subprocess -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs - - -def main(): - - # Run the Testcases: - test = test_gbp_pr_neg() - if test.test_gbp_pr_neg_1() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PR_NEG_1') - if test.test_gbp_pr_neg_2() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PR_NEG_2') - if test.test_gbp_pr_neg_3() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PR_NEG_3') - if test.test_gbp_pr_neg_4() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PR_NEG_4') - test.cleanup() - utils_libs.report_results('test_gbp_pr_neg', 'test_results.txt') - sys.exit(1) - - -class test_gbp_pr_neg(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_pr_neg.log' - subprocess.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_pr_neg.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self._log.info("\n START OF GBP POLICY_RULE NEGATIVE TESTSUITE") - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.act_name = 'demo_pa' - self.cls_name = 'demo_pc' - self.rule_name = 'demo_pr' - self._log.info('\n## Step 1: Create a PC needed for PR Testing ##') - self.cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', self.cls_name) - if self.cls_uuid == 0: - self._log.info( - "\nReqd Classifier Create Failed, hence GBP Policy Rule " - "Negative Test Suite Run ABORTED\n") - os._exit(1) - self._log.info('\n## Step 1: Create a PA needed for PR Testing ##') - self.act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', self.act_name) - if self.act_uuid == 0: - self._log.info( - "\nReqd Action Create Failed, hence GBP Policy Rule " - "Negative Test Suite Run ABORTED\n") - os._exit(1) - - def cleanup(self, tc_name=''): - if tc_name != '': - self._log.info('Testcase %s: FAILED' % (tc_name)) - for obj in ['rule', 'classifier', 'action']: - self.gbpcfg.gbp_del_all_anyobj(obj) - - def test_gbp_pr_neg_1(self): - - self._log.info( - "\n###################################################\n" - "TESTCASE_GBP_PR_NEG_1: TO CREATE/VERIFY a POLICY RULE with " - "INVALID PC\n" - "TEST_STEP::\n" - "Create Policy Rule Object with Invalid PC\n" - "Verify PR creation failed and was rolled back\n" - "###################################################\n") - - # Testcase work-flow starts - self._log.info("\n## Step 1: Create Rule with Invalid PC##\n") - if self.gbpcfg.gbp_policy_cfg_all( - 1, 'rule', self.rule_name, classifier="INVALID") != 0: - self._log.info( - "\n## Step 1: Create Policy Rule with Invalid Policy " - "Classifier did NOT Fail") - return 0 - self._log.info("\n## Step 1A: Verify Policy Rule has been rolled back") - if self.gbpverify.gbp_policy_verify_all( - 1, 'rule', self.rule_name) != 0: - self._log.info( - "\n## Step 1B: Verify Policy Rule did NOT roll back") - return 0 - self._log.info("\n## TESTCASE_GBP_PR_NEG_1: PASSED") - return 1 - - def test_gbp_pr_neg_2(self): - - self._log.info( - "\n#################################################\n" - "TESTCASE_GBP_PR_NEG_2: TO CREATE/VERIFY/ POLICY RULE with " - "VALIC PC but INVALID PA\n" - "TEST_STEP::\n" - "Create Policy Rule Object with Valid PC but Invalid PA\n" - "Verify the Policy Rule creation fails and config is rolled back\n" - "#################################################\n") - - # Testcase work-flow starts - self._log.info( - "\n## Step 1: Create Policy Rule with Valid PC & Invalid PA ##") - if self.gbpcfg.gbp_policy_cfg_all( - 1, - 'rule', - self.rule_name, - classifier=self.cls_name, - action='INVALID') != 0: - self._log.info( - "\n## Step 1: Create Policy Rule with Invalid PA did NOT Fail") - return 0 - self._log.info("\n## Step 1A: Verify Policy Rule has been rolled back") - if self.gbpverify.gbp_policy_verify_all( - 1, 'rule', self.rule_name) != 0: - self._log.info( - "\n## Step 1A: Verify Policy Rule did NOT roll back") - return 0 - self._log.info("\n## TESTCASE_GBP_PR_NEG_2: PASSED") - return 1 - - def test_gbp_pr_neg_3(self): - - self._log.info( - "\n################################################\n" - "TESTCASE_GBP_PR_NEG_3: TO CREATE/UPDATE/VERIFY/ POLICY RULE " - "with Invalid PC and PA ##\n" - "TEST_STEP::\n" - "Create Policy Rule with Valid PC and Valid PR\n" - "Update the Policy Rule's PA by an Invalid PA\n" - "Verify the Policy Rule's Update failed and config rolled back " - "to original attr values\n" - "Update the Policy Rule's PC by an Invalid PC\n" - "Verify the Policy Rule's Update failed and config rolled back " - "to original attr values\n" - "#################################################\n") - - # Testcase work-flow starts - self._log.info('\n## Step 1: Create Policy Rule with PA and PC##\n') - rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'rule', self.rule_name, classifier=self.cls_name, - action=self.act_name) - if rule_uuid != 0: - self._log.info( - "Step 1: Create Rule Passed, UUID == %s\n" % - (rule_uuid)) - else: - self._log.info("# Step 1: Create Rule == Failed") - return 0 - self._log.info( - '\n## Step 2: Update Policy Rule with Invalid PA and Invalid ' - 'PC one at a time ##\n') - attrib_list = [{'classifier': 'INVALID'}, {'action': 'INVALID'}] - for attr_val in attrib_list: - if self.gbpcfg.gbp_policy_cfg_upd_all( - 'rule', rule_uuid, attr_val) != 0: - self._log.info( - "\nStep 2: Updating Policy Rule's Attribute %s with " - "Invalid Value did NOT Fail" % - (attr_val)) - return 0 - # Verify starts - if self.gbpverify.gbp_policy_verify_all( - 1, - 'rule', - rule_uuid, - name=self.rule_name, - policy_classifier_id=self.cls_uuid, - policy_actions=self.act_uuid) == 0: - self._log.info( - "# Step 2B: Verify Policy Rule Updated did NOT roll back") - return 0 - self._log.info("\n## TESTCASE_GBP_PR_NEG_3: PASSED") - return 1 - - def test_gbp_pr_neg_4(self): - - self._log.info( - "\n###############################################\n" - "TESTCASE_GBP_PR_NEG_4: DELETE NON-EXISTENT/INVALID POLICY RULE \n" - "TEST_STEP::\n" - "Delete unknown/invalid policy-rule\n" - "##############################################\n") - - self._log.info("\n## Step 1: Delete non-existent Polic Rule ##") - if self.gbpcfg.gbp_policy_cfg_all(0, 'rule', 'INVALID') != 0: - self._log.info( - "\n## Step 1: Delete Non-existent policy rule did NOT Fail") - return 0 - self._log.info("\n## TESTCASE_GBP_PR_NEG_4: PASSED") - return 1 - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pr_pc_pa_shared_func.py b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pr_pc_pa_shared_func.py deleted file mode 100755 index f0156026d..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_pr_pc_pa_shared_func.py +++ /dev/null @@ -1,557 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import subprocess -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs -import six - - -def main(): - # Run the Testcases: - test = test_gbp_pr_pc_pa_shared_func() - if test.test_gbp_pr_pc_pa_shared_func_1() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PR_PC_PA_SHARED_INTEG_1') - if test.test_gbp_pr_pc_pa_shared_func_2() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PR_PC_PA_SHARED_INTEG_2') - if test.test_gbp_pr_pc_pa_shared_func_3() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PR_PC_PA_SHARED_INTEG_3') - if test.test_gbp_pr_pc_pa_shared_func_4() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PR_PC_PA_SHARED_INTEG_4') - test.cleanup() - utils_libs.report_results('test_gbp_pr_pc_pa_shared_func', - 'test_results_admin.txt') - sys.exit(1) - - -class test_gbp_pr_pc_pa_shared_func(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_pr_pc_pa_shared_func.log' - subprocess.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_pr_pc_pa_shared_func.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self._log.info( - "\n## START OF GBP POLICY_RULE,POLICY_CLASS,POLICY_ACTION SHARED " - "RESOURCE INTEGRITY TESTSUITE\n") - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.act_name = 'demo_pa' - self.cls_name = 'demo_pc' - self.rule_name = 'demo_pr' - - def cleanup(self, tc_name=''): - if tc_name != '': - self._log.info('%s: FAILED' % (tc_name)) - for obj in ['rule', 'classifier', 'action']: - self.gbpcfg.gbp_del_all_anyobj(obj) - - def test_gbp_pr_pc_pa_shared_func_1(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PR_PC_PA_SHARED_INTEG_1: " - "TO CREATE/VERIFY/UPDATEE/VERIFY INTEGRITY " - "B/W PR,PC,PA as 'shared' \n" - "TEST_STEP::\n" - "Create Policy Action,Policy Class & Policy Rule " - "with param shared=True\n" - "Verify the attributes & value, show & list cmds\n" - "Update the Policy Rule param shared=False\n" - "Verify that PR param shared got updated\n" - "##############################################################\n") - - # Testcase work-flow starts - # Create PA,PC,PR - self._log.info('\n## Step 1A: Create a PC with shared=True ##') - obj_uuid = {} - self.cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', self.cls_name, shared=True) - if self.cls_uuid == 0: - self._log.info( - "\n Creation of Policy Classifier with shared=True, Failed\n") - return 0 - obj_uuid['classifier'] = self.cls_uuid - self._log.info('\n## Step 1B: Create a PA with shared=True ##') - self.act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', self.act_name, shared=True) - if self.act_uuid == 0: - self._log.info( - "\n Creation of Policy Action with shared=True, Failed\n") - return 0 - obj_uuid['action'] = self.act_uuid - self._log.info( - "\n## Step 1C: Create Policy Rule with PC & PA with " - "shared=True ##") - rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'rule', - self.rule_name, - classifier=self.cls_uuid, - action=self.act_uuid, - shared=True) - if rule_uuid == 0: - self._log.info( - "# Step 1: Creation of Policy Rule with shared=True, Failed") - return 0 - # Verify PA,PC,PR - self._log.info( - '## Step 2: Verify Policy Rule, Policy Classifier, Policy ' - 'Action with shared=True') - for obj, uuid in six.iteritems(obj_uuid): - if self.gbpverify.gbp_policy_verify_all( - 1, obj, uuid, shared=True) == 0: - self._log.info( - "# Step 2A_%s: Verify Policy %s.upper() with " - "shared=True, Failed" % - (obj, obj)) - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'rule', - self.rule_name, - id=rule_uuid, - policy_classifier_id=self.cls_uuid, - enabled='True', - policy_actions=self.act_uuid, - shared=True) == 0: - self._log.info( - "# Step 2B: Verify Policy Rule with shared=True, Failed") - return 0 - # Update PR - self._log.info("\n## Update the Policy Rule with shared=False ##\n") - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'rule', rule_uuid, shared=False) == 0: - self._log.info( - "\nStep 3: Updating Policy Rule's shared=False, Failed") - return 0 - # Verify the PR after update - if self.gbpverify.gbp_policy_verify_all( - 1, 'rule', rule_uuid, shared=False) == 0: - self._log.info( - "# Step 4: Verify Policy Rule with shared=False, Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_PR_PC_PA_SHARED_INTEG_1: PASSED") - self.cleanup() - return 1 - - def test_gbp_pr_pc_pa_shared_func_2(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PR_PC_PA_SHARED_INTEG_2: TO CREATE/UPDATE/ " - "POLICY RULE with shared/non-shared PA & PCs\n" - "TEST_STEP::\n" - "Create Policy Classifier and Policy Action with " - "shared=False(default)\n" - "Create another set of Policy Classifier & Policy " - "Action with shared=True\n" - "Create Policy Rule using the 2nd set of PA & PC " - "and shared=True\n" - "Update the Policy Rule with 1st set of PA & PC " - "and verify update failed\n" - "Update the Policy Rule by setting shared=False " - "and verify it passed\n" - "##############################################################\n") - - # Testcase work-flow starts - # Create PA,PC with shared=False - self._log.info( - '\n## Step 1A: Create a PC with shared=False(default) ##') - obj_uuid_false = {} - self.cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', 'cls_shared') - if self.cls_uuid == 0: - self._log.info( - "\n Creation of Policy Classifier with shared=False, Failed\n") - return 0 - obj_uuid_false['classifier'] = self.cls_uuid - self._log.info( - '\n## Step 1B: Create a PA with shared=False(default) ##') - self.act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', 'act_shared') - if self.act_uuid == 0: - self._log.info( - "\n Creation of Policy Action with shared=False, Failed\n") - return 0 - obj_uuid_false['action'] = self.act_uuid - # Create PA,PC with shared=True - self._log.info('\n## Step 2A: Create a PC with shared=True ##') - obj_uuid_true = {} - self.cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', self.cls_name, shared=True) - if self.cls_uuid == 0: - self._log.info( - "\n Step 2A: Creation of Policy Classifier with " - "shared=True, Failed\n") - return 0 - obj_uuid_true['classifier'] = self.cls_uuid - self._log.info('\n## Step 2B: Create a PA with shared=True ##') - self.act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', self.act_name, shared=True) - if self.act_uuid == 0: - self._log.info( - "\n Step 2B: Creation of Policy Action with " - "shared=True, Failed\n") - return 0 - obj_uuid_true['action'] = self.act_uuid - # Create PR(shared=True) with PA+PC(shared=True) - self._log.info( - "\n## Step 3: Create Policy Rule with PC & PA with shared=True ##") - rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'rule', - 'pr_true', - classifier=obj_uuid_true['classifier'], - action=obj_uuid_true['action'], - shared=True) - if rule_uuid == 0: - self._log.info( - "# Step 3: Creation of Policy Rule with shared=True " - "using attributes PA+PC(shared=True), Failed") - return 0 - # Update and Verify the PR(shared=True) with PA+PC(shared=False) - self._log.info( - "\n## Step 4A: Update the Policy Rule with PC & PA which " - "are with shared=False ##") - if self.gbpcfg.gbp_policy_cfg_all( - 2, - 'rule', - rule_uuid, - classifier=obj_uuid_false['classifier'], - action=obj_uuid_false['action']) != 0: - self._log.info( - "# Step 4A: Updating Policy Rule(shared=True) by attributes " - "PA+PC(shared=False) DID NOT Fail") - return 0 - self._log.info( - "\n## Step 4B: Verify the Policy Rule initial attributes " - "PA,PC,shared=True ##") - if self.gbpverify.gbp_policy_verify_all( - 1, - 'rule', - 'pr_true', - id=rule_uuid, - policy_classifier_id=obj_uuid_true['classifier'], - enabled='True', - policy_actions=obj_uuid_true['action'], - shared=True) == 0: - self._log.info( - "# Step 4B: Verify Policy Rule with shared=True, Failed") - return 0 - # Update and Verify the PR(shared=False) with PA+PC(shared=False) - self._log.info( - "\n## Step 5A: Update the Policy Rule's shared=False along " - "with PC+PA(shared=False) ##") - if self.gbpcfg.gbp_policy_cfg_all( - 2, - 'rule', - rule_uuid, - classifier=obj_uuid_false['classifier'], - action=obj_uuid_false['action'], - shared=False) == 0: - self._log.info( - "# Step 5A: Updating Policy Rule(shared=False) by " - "attributes PA+PC(shared=False), Failed") - return 0 - self._log.info( - "\n## Step 5B: Verify the Policy Rule attributes " - "PA,PC,shared=False ##") - if self.gbpverify.gbp_policy_verify_all( - 1, - 'rule', - 'pr_true', - id=rule_uuid, - policy_classifier_id=obj_uuid_false['classifier'], - enabled='True', - policy_actions=obj_uuid_false['action'], - shared=False) == 0: - self._log.info( - "# Step 5B: Verify Policy Rule with shared=False, Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_PR_PC_PA_SHARED_INTEG_2: PASSED") - self.cleanup() - return 1 - - def test_gbp_pr_pc_pa_shared_func_3(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PR_PC_PA_SHARED_INTEG_3: TO " - "CREATE/VERIFY//VERIFY a POLICY RULE with POLICY " - "ACTION & CLASSIFIER\n" - "TEST_STEP::\n" - "Create Policy Action with shared=False, while Policy " - "Classifier with shared=True\n" - "Create Policy Rule with PA & PC and shared=True and " - "verify it failed to create\n" - "Retry Create Policy Rule with above PA,PC and " - "shared=False and verify it success\n" - "Update the Policy Action with shared=True \n" - "Create Policy Rule with shared=True with above PA+PC " - "and verify it success\n" - "##############################################################\n") - - # Testcase work-flow starts - # Create PA & PC with shared+False & True resp - self._log.info('\n## Step 1A: Create a PC with shared=True ##') - obj_uuid = {} - self.cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', 'cls_true', shared=True) - if self.cls_uuid == 0: - self._log.info( - "\n Creation of Policy Classifier with shared=False, Failed\n") - return 0 - obj_uuid['classifier'] = self.cls_uuid - self._log.info('\n## Step 1B: Create a PA with shared=False ##') - self.act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', 'act_false') - if self.act_uuid == 0: - self._log.info( - "\n Creation of Policy Action with shared=False, Failed\n") - return 0 - obj_uuid['action'] = self.act_uuid - # Create/Retry a PR using above PA & PC, once with shared= True & False - self._log.info( - "\n## Step 2A: Create Policy Rule(shared=True) with " - "PA(shared=False) & PC(shared=True) ##") - if self.gbpcfg.gbp_policy_cfg_all( - 1, - 'rule', - 'true_pr', - classifier=obj_uuid['classifier'], - action=obj_uuid['action'], - shared=True) != 0: - self._log.info( - "# Step 2A: Creation of Policy Rule with shared=True using " - "attributes PA(shared=False)+PC(shared=True) did NOT Fail") - return 0 - self._log.info( - "\n## Step 2B: Create Policy Rule(shared=False) with " - "PA(shared=False) & PC(shared=True)& Verify ##") - rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'rule', - 'true_pr', - classifier=obj_uuid['classifier'], - action=obj_uuid['action']) - if rule_uuid == 0: - self._log.info( - "# Step 2B: Creation of Policy Rule with " - "shared=False(default) using attributes " - "PA(shared=False)+PC(shared=True), Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'rule', - rule_uuid, - policy_classifier_id=obj_uuid['classifier'], - enabled='True', - policy_actions=obj_uuid['action'], - shared=False) == 0: - self._log.info( - "# Step 2C: Verify Policy Rule with shared=False, Failed") - return 0 - # Update the Policy Action with shared=True - self._log.info( - "\n## Step 3: Update the Policy Action with shared=True\n") - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'action', self.act_uuid, shared=True) == 0: - self._log.info( - "# Step 3A: Update of Policy Action shared=True, Failed") - return 0 - # Create and Verify a PR with shared=True using above - # PA+PC(shared=True) - self._log.info( - "\n## Step 4: Create and Verify a PR with shared=True " - "using above PA+PC(shared=True)\n") - true_rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'rule', - 'true_pr', - classifier=obj_uuid['classifier'], - action=obj_uuid['action'], - shared=True) - if true_rule_uuid == 0: - self._log.info( - "# Step : Creation of Policy Rule with shared=True " - "using attributes PA+PC(shared=True), Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'rule', - true_rule_uuid, - policy_classifier_id=obj_uuid['classifier'], - enabled='True', - policy_actions=obj_uuid['action'], - shared=True) == 0: - self._log.info( - "# Step 4B: Verify Policy Rule with shared=True, Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_PR_PC_PA_SHARED_INTEG_3: PASSED") - self.cleanup() - return 1 - - def test_gbp_pr_pc_pa_shared_func_4(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PR_PC_PA_SHARED_INTEG_4: TO " - "CREATE/VERIFY/UPDATE/VERIFY a POLICY ACTION & " - "CLASSIFIER for a POLICY RULE\n" - "TEST_STEP::\n" - "Create Policy Action & Policy Classifier with shared=False\n" - "Create Policy Rule with PA & PC and shared=True " - "and verify creation fails\n" - "Update the above Policy Action & Classifier with " - "shared=True and verify it success\n" - "Create the Policy Rule with shared=True using above PA & PC \n" - "Update Policy Action and Classifier with " - "shared=False and verify it failed to upudate\n" - "##############################################################\n") - - # Testcase work-flow starts - # Create PA,PC with shared=False - self._log.info( - '\n## Step 1A: Create a PC with shared=False(default) ##') - obj_uuid_false = {} - self.cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', 'cls_false') - if self.cls_uuid == 0: - self._log.info( - "\n Creation of Policy Classifier with shared=False, Failed\n") - return 0 - obj_uuid_false['classifier'] = self.cls_uuid - self._log.info( - '\n## Step 1B: Create a PA with shared=False(default) ##') - self.act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', 'act_false') - if self.act_uuid == 0: - self._log.info( - "\n Creation of Policy Action with shared=False, Failed\n") - return 0 - obj_uuid_false['action'] = self.act_uuid - # Create & Verify PR with shared=True and above PA & PC and create - # should fail - self._log.info( - "\n## Step 2B: Create Policy Rule(shared=True) " - "with PA(shared=False) & PC(shared=True)& create fails ##") - if self.gbpcfg.gbp_policy_cfg_all( - 1, - 'rule', - 'pr_true', - classifier=obj_uuid_false['classifier'], - action=obj_uuid_false['action'], - shared=True) != 0: - self._log.info( - "# Step 2B: Creation of Policy Rule with " - "shared=True using attributes PA+PC(shared=False) " - "DID NOT Fail") - return 0 - # Update the Policy Action & Policy CLassifier with shared=True - self._log.info( - "\n## Step 3A: Update the Policy Action with shared=True\n") - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'action', self.act_uuid, shared=True) == 0: - self._log.info( - "# Step 3A: Update of Policy Action shared=True, Failed") - return 0 - self._log.info( - "\n## Step 3B: Update the Policy Classifer with shared=True\n") - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'classifier', self.cls_uuid, shared=True) == 0: - self._log.info( - "# Step 3B: Update of Policy Classifier shared=True, Failed") - return 0 - # Create and verify the Policy Rule with shared=True - self._log.info( - "\n## Step 4: Create the Policy Rule with shared=True using " - "PA+PC(shared=True)\n") - rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'rule', - 'pr_true', - classifier=obj_uuid_false['classifier'], - action=obj_uuid_false['action'], - shared=True) - if rule_uuid == 0: - self._log.info( - "# Step 4: Create of Policy Rule shared=True, Failed") - return 0 - self._log.info( - "\n## Step 4A: Verify the Policy Rule got updated shared=True\n") - if self.gbpverify.gbp_policy_verify_all( - 1, - 'rule', - rule_uuid, - policy_classifier_id=obj_uuid_false['classifier'], - enabled='True', - policy_actions=obj_uuid_false['action'], - shared=True) == 0: - self._log.info( - "# Step 4A: Verify Policy Rule with shared=True, Failed") - return 0 - # Update Policy Action and Classifier with shared=False and verify it - # failed to upudate - self._log.info( - "\n## Step 5A: Update the Policy Action with shared=False\n") - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'action', self.act_uuid, shared=False) != 0: - self._log.info( - "# Step 5A: Update of Policy Action shared=False " - "DID NOT Fail") - return 0 - self._log.info( - "\n## Step 5B: Update the Policy Classifer with shared=False\n") - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'classifier', self.cls_uuid, shared=False) != 0: - self._log.info( - "# Step 5B: Update of Policy Classifier shared=False " - "DID NOT Fail") - return 0 - # Verify the shared attributes of Policy Action & Classifier as True - if self.gbpverify.gbp_policy_verify_all( - 1, 'action', self.act_uuid, shared='True') == 0: - self._log.info( - "# Step 6: Policy Action verify shows that shared " - "attribute changed to False") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, 'classifier', self.cls_uuid, shared='True') == 0: - self._log.info( - "# Step 6: Policy Classifier verify shows that shared " - "attribute changed to False") - return 0 - self._log.info("\n## TESTCASE_GBP_PR_PC_PA_SHARED_INTEG_4: PASSED") - self.cleanup() - return 1 - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_func.py b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_func.py deleted file mode 100755 index 441cdd5cf..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_func.py +++ /dev/null @@ -1,643 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import subprocess -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs - - -def main(): - # Run the Testcases: - test = test_gbp_prs_func() - if test.test_gbp_prs_func_1() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PRS_FUNC_1') - if test.test_gbp_prs_func_2() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PRS_FUNC_2') - if test.test_gbp_prs_func_3() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PRS_FUNC_3') - if test.test_gbp_prs_func_4() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PRS_FUNC_4') - if sys.argv[1] == 'aci': - test._log.info( - "\nTESTCASE_GBP_PRS_FUNC_5: TO CREATE/VERIFY/DELETE/VERIFY " - "a PARENT and CHILD POLICY RULESET\n") - test._log.info("\nTESTCASE_GBP_PRS_FUNC_5: NOT SUPPORTED in ACI") - test._log.info( - "\nTESTCASE_GBP_PRS_FUNC_6: TO CHANGE/UPDATE/DELETE/VERIFY " - "PARENT and CHILD POLICY RULESET\n") - test._log.info("\nTESTCASE_GBP_PRS_FUNC_6: NOT SUPPORTED in ACI") - else: - if test.test_gbp_prs_func_5() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PRS_FUNC_5') - if test.test_gbp_prs_func_6() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PRS_FUNC_6') - test.cleanup() - utils_libs.report_results('test_gbp_prs_func', 'test_results.txt') - sys.exit(1) - - -class test_gbp_prs_func(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_prs_func.log' - subprocess.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_prs_func.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self._log.info( - "\n## START OF GBP POLICY_RULE_SET FUNCTIONALITY TESTSUITE\n") - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.act_name = 'demo_pa' - self.cls_name = 'demo_pc' - self.rule_name = 'demo_pr' - self.ruleset_name = 'demo_prs' - self._log.info('\n## Step 1: Create a PC needed for PRS Testing ##') - self.cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', self.cls_name) - if self.cls_uuid == 0: - self._log.info( - "\nReqd Policy Classifier Create Failed, hence GBP " - "Policy Rule-Set Functional Test Suite Run ABORTED\n") - return - self._log.info('\n## Step 1: Create a PA needed for PRS Testing ##') - self.act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', self.act_name) - if self.act_uuid == 0: - self._log.info( - "\nReqd Policy Action Create Failed, hence GBP " - "Policy Rule-Set Functional Test Suite Run ABORTED\n") - return - self._log.info('\n## Step 1: Create a PR needed for PRS Testing ##') - self.rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'rule', self.rule_name, classifier=self.cls_name, - action=self.act_name) - if self.rule_uuid == 0: - self._log.info( - "\nReqd Policy Rule Create Failed, hence GBP " - "Policy Rule-Set Functional Test Suite Run ABORTED\n ") - return - - def cleanup(self, tc_name=''): - if tc_name != '': - self._log.info('%s: FAILED' % (tc_name)) - for obj in ['ruleset', 'rule', 'classifier', 'action']: - self.gbpcfg.gbp_del_all_anyobj(obj) - - def test_gbp_prs_func_1( - self, - name_uuid='', - ruleset_uuid='', - rep_cr=0, - rep_del=0): - - if rep_cr == 0 and rep_del == 0: - self._log.info( - "\n########################################################\n" - "TESTCASE_GBP_PRS_FUNC_1: TO CREATE/VERIFY/DELETE/VERIFY " - "a POLICY RULESET with DEFAULT ATTRIB VALUE\n" - "TEST_STEP::\n" - "Create Policy RuleSet Object\n" - "Verify the attributes & value, show & list cmds\n" - "Delete Policy RuleSet using Name\n" - "Verify that PR has got deleted, show & list cmds\n" - "##########################################################\n") - - if name_uuid == '': - name_uuid = self.ruleset_name - # Testcase work-flow starts - if rep_cr == 0 or rep_cr == 1: - self._log.info( - '\n## Step 1: Create RuleSet with default attrib vals##\n') - ruleset_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'ruleset', name_uuid) - if ruleset_uuid == 0: - self._log.info("# Step 1: Create RuleSet == Failed") - return 0 - self._log.info('# Step 2A: Verify RuleSet using -list cmd') - if self.gbpverify.gbp_policy_verify_all( - 0, 'ruleset', name_uuid, ruleset_uuid) == 0: - self._log.info( - "# Step 2A: Verify RuleSet using -list option == Failed") - return 0 - self._log.info('# Step 2B: Verify RuleSet using -show cmd') - if self.gbpverify.gbp_policy_verify_all( - 1, 'ruleset', name_uuid, id=ruleset_uuid, - shared='False') == 0: - self._log.info( - "# Step 2B: Verify RuleSet using -show option == Failed") - return 0 - ####### - if rep_del == 0 or rep_del == 1: - self._log.info('\n## Step 3: Delete RuleSet using name ##\n') - if self.gbpcfg.gbp_policy_cfg_all(0, 'ruleset', name_uuid) == 0: - self._log.info("# Step 3: Delete RuleSet == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 0, 'ruleset', name_uuid, ruleset_uuid) != 0: - self._log.info( - "# Step 3A: Verify RuleSet is Deleted using -list " - "option == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, 'ruleset', name_uuid, id=ruleset_uuid, - shared='False') != 0: - self._log.info( - "# Step 3B: Verify RuleSet is Deleted using " - "-show option == Failed") - return 0 - if rep_cr == 0 and rep_del == 0: - self._log.info("\nTESTCASE_GBP_PRS_FUNC_1: PASSED") - return 1 - - def test_gbp_prs_func_2(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PRS_FUNC_2: TO CREATE/VERIFY/DELETE/VERIFY " - "a POLICY RULESET with POLICY RULE\n" - "TEST_STEP::\n" - "Create Policy RuleSet Object with GBP PR\n" - "Verify the attributes & value, show & list cmds\n" - "Delete Policy RuleSet using Name\n" - "Verify that PR has got deleted, show & list cmds\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info("\n## Step 1: Create Policy RuleSet with PR ##") - ruleset_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'ruleset', self.ruleset_name, policy_rules=self.rule_name) - if ruleset_uuid == 0: - self._log.info("# Step 1: Create RuleSet == Failed") - return 0 - self._log.info('# Step 2A: Verify RuleSet using -list cmd') - if self.gbpverify.gbp_policy_verify_all( - 0, 'ruleset', self.ruleset_name, ruleset_uuid) == 0: - self._log.info( - "# Step 2A: Verify RuleSet using -list option == Failed") - return 0 - self._log.info('# Step 2B: Verify RuleSet using -show cmd') - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - self.ruleset_name, - id=ruleset_uuid, - policy_rules=self.rule_uuid, - shared='False') == 0: - self._log.info( - "# Step 2B: Verify RuleSet using -show option == Failed") - return 0 - self.test_gbp_prs_func_1(ruleset_uuid=ruleset_uuid, rep_cr=2) - self._log.info("\nTESTCASE_GBP_PRS_FUNC_2: PASSED") - return 1 - - def test_gbp_prs_func_3(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PRS_FUNC_3: TO UPDATE/VERIFY/DELETE/VERIFY " - "EACH ATTRIB of a POLICY RULESET\n" - "TEST_STEP::\n" - "Create Policy RuleSet using Default param values\n" - "Update Each the Policy Rule's editable params\n" - "Verify the Policy Rule's attributes & values, show & list cmds\n" - "Delete the Policy Rule\n" - "Verify Policy RuleSet successfully deleted\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info('\n## Step 1: Create Policy RuleSet with PR ##\n') - ruleset_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'ruleset', self.ruleset_name, policy_rules=self.rule_name) - if ruleset_uuid != 0: - self._log.info( - "Step 1: Create RuleSet Passed, UUID == %s\n" % - (ruleset_uuid)) - else: - self._log.info("# Step 1: Create RuleSet == Failed") - return 0 - self._log.info('\n## Step 1A: Create new PA ,new PC, new PR##\n') - new_cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', 'grppol_pc1') - if new_cls_uuid == 0: - self._log.info( - "\nNew Classifier Create Failed, hence " - "testcase_gbp_prs_func_3 ABORTED\n") - return 0 - new_act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', 'grppol_pa1') - if new_act_uuid == 0: - self._log.info( - "\nNew Action Create Failed, hence " - "testcase_gbp_prs_func_3 ABORTED\n") - return 0 - new_rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'rule', - 'grppol_pr', - classifier=new_cls_uuid, - action=new_act_uuid, - description="'For devstack demo'") - if new_rule_uuid == 0: - self._log.info( - "\nNew Rule Create Failed, hence " - "testcase_gbp_prs_func_3 ABORTED\n") - return 0 - if self.gbpcfg.gbp_policy_cfg_all( - 2, - 'ruleset', - ruleset_uuid, - name='grppol_prs', - policy_rule=new_rule_uuid, - description="'For devstack demo'") == 0: - self._log.info( - "\nStep 2: Updating Policy RuleSet's Attributes , Failed") - return 0 - # Verify starts - if self.gbpverify.gbp_policy_verify_all( - 0, 'ruleset', 'grppol_prs', ruleset_uuid) == 0: - self._log.info( - "# Step 2A: Verify Policy RuleSet Updated " - "Attributes using -list option == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - ruleset_uuid, - name='grppol_prs', - policy_rules=new_rule_uuid, - description='For devstack demo') == 0: - self._log.info( - "# Step 2B: Verify Policy RuleSet Updated " - "Attributes using -show option == Failed") - return 0 - self._log.info("\nTESTCASE_GBP_PRS_FUNC_3: PASSED") - return 1 - - def test_gbp_prs_func_4(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PRS_FUNC_4: TO CREATE/UPDATE/VERIFY/DELETE/ " - "ASSOCIATING MULTIPLE PRs to 1 POLICY RULESET \n" - "TEST_STEP::\n" - "Create Multiple Policy Rules\n" - "Create Policy RuleSet by associating all the Policy Rules\n" - "Verify that multiple Policy Rules are associated to the " - "Policy RuleSet\n" - "Update the Policy RuleSet such that few Policy Rules " - "are unmapped\n" - "Verify the Policy Rule's attributes & values, show & list cmds\n" - "Update the Policy RuleSet such that all Policy Rules " - "association removed\n" - "Verify the Policy Rule's attributes & values, show & list cmds\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info( - '\n## Step 1A: Create new PA ,new PC, 4 PRs using the ' - 'same PA & PC##\n') - new_cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', 'grppol_pc1') - if new_cls_uuid == 0: - self._log.info( - "\nNew Classifier Create Failed, hence " - "testcase_gbp_prs_func_4 ABORTED\n") - return 0 - new_act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', 'grppol_pa1') - if new_act_uuid == 0: - self._log.info( - "\nNew Action Create Failed, hence " - "testcase_gbp_prs_func_4 ABORTED\n") - return 0 - rule_uuid_list = [] - for i in range(4): - new_rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'rule', - 'grppol_pr_%s' % - (i), - classifier=new_cls_uuid, - action=new_act_uuid, - description="'For devstack demo'") - if new_rule_uuid == 0: - self._log.info( - "\nNew Rule Create Failed, hence " - "testcase_gbp_prs_func_4 ABORTED\n") - return 0 - rule_uuid_list.append(new_rule_uuid) - ruleset_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'ruleset', - 'grppol_prs_many', - policy_rule='"%s %s %s %s"' % - (rule_uuid_list[0], - rule_uuid_list[1], - rule_uuid_list[2], - rule_uuid_list[3]), - description="'For devstack demo'") - if ruleset_uuid == 0: - self._log.info( - "\nStep 2: Updating Policy RuleSet's Attributes , Failed") - return 0 - # Verify starts - if self.gbpverify.gbp_policy_verify_all( - 0, 'ruleset', 'grppol_prs_many', ruleset_uuid) == 0: - self._log.info( - "# Step 2A: Verify Policy RuleSet Updated Attributes " - "using -list option == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - ruleset_uuid, - name='grppol_prs_many', - description='For devstack demo') == 0: - self._log.info( - "# Step 2B: Verify Policy RuleSet Updated Attributes " - "using -show option == Failed") - return 0 - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'ruleset', 'grppol_prs_many', 'policy_rules', - rule_uuid_list) == 0: - self._log.info( - "# Step 2C: Verify Policy RuleSet and its Multiple PRs " - "using -show option == Failed") - return 0 - # Update and Verify the PRS by updating the PRs(removing few existing - # ones) - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'ruleset', 'grppol_prs_many', policy_rule='"%s %s"' % - (rule_uuid_list[0], rule_uuid_list[2])) == 0: - self._log.info( - "# Step 3: Updating Policy RuleSet's Attributes , Failed") - return 0 - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'ruleset', 'grppol_prs_many', 'policy_rules', - rule_uuid_list) != 0: - self._log.info( - "# Step 3A: Verify Policy RuleSet and its Multiple " - "PRs using -show option == Failed") - return 0 - # Update and Verify the PRS by updating the PRs=NULL(unmapping all PRs) - if self.gbpcfg.gbp_policy_cfg_all(2, 'ruleset', 'grppol_prs_many', - policy_rule='""') == 0: - self._log.info( - "# Step 4: Upmapping All Policy Rule from Policy " - "RuleSet , Failed") - return 0 - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'ruleset', 'grppol_prs_many', 'policy_rules', - rule_uuid_list) != 0: - self._log.info( - "# Step 4A: Verify All Policy Rules have been Removed " - "from Policy RuleSet using --show option == Failed") - return 0 - self._log.info("\nTESTCASE_GBP_PRS_FUNC_4: PASSED") - return 1 - - def test_gbp_prs_func_5(self): - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PRS_FUNC_5: TO CREATE/VERIFY/DELETE/VERIFY " - "a PARENT and CHILD POLICY RULESET\n" - "TEST_STEP::\n" - "Create 1 Policy RuleSet using the same PA & PC\n" - "Create the 2nd Policy RuleSet using the same PA, " - "PC and associate PRS-1 as CHILD\n" - "Verify the Child PRS reflect the Parent PRS and viceversa\n" - "Delete the Child PRS\n" - "Verify the Parent PRS has no CHILD\n" - "Create the CHild PRS, associate to the Parent PRS\n" - "Verify the association is established b/e Child and Parent\n" - "Delete the Parent PRS\n" - "Verify the Parent PRS association removed the Child PRS\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info("\n## Step 1: Create Policy RuleSet with PR ##") - child_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'ruleset', 'demo_child_prs', policy_rules=self.rule_name) - if child_uuid == 0: - self._log.info( - "\n## Step 1: Create Child Policy RuleSet == Failed") - return 0 - parent_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'ruleset', - 'demo_par_prs', - policy_rules=self.rule_name, - child_policy_rule_sets=child_uuid) - if parent_uuid == 0: - self._log.info( - "\n## Step 2: Create Parent Policy RuleSet == Failed") - return 0 - self._log.info( - '# Step 2A: Verify Parent and Child Policy RuleSet ' - 'using -show cmd') - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - 'demo_par_prs', - id=parent_uuid, - policy_rules=self.rule_uuid, - shared='False', - child_policy_rule_sets=child_uuid) == 0: - self._log.info( - "\n## Step 2A: Verify Parent RuleSet using -show " - "option == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - 'demo_child_prs', - id=child_uuid, - policy_rules=self.rule_uuid, - shared='False', - parent_id=parent_uuid) == 0: - self._log.info( - "\n## Step 2B: Verify Parent RuleSet using -show " - "option == Failed") - return 0 - # Delete Child PRS - if self.gbpcfg.gbp_policy_cfg_all(0, 'ruleset', 'demo_child_prs') == 0: - self._log.info("# Step 3: Delete Child Policy RuleSet == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - 'demo_par_prs', - id=parent_uuid, - policy_rules=self.rule_uuid, - shared='False', - child_policy_rule_sets=child_uuid) != 0: - self._log.info( - "# Step 3A: Verify Parent PRS after Delete of Child " - "PRS using -show option == Failed") - return 0 - # Create Child PRS,Associate to Parent and Verify - child_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'ruleset', 'demo_child_prs', policy_rules=self.rule_name) - if child_uuid == 0: - self._log.info("# Step 4: Create Child Policy RuleSet == Failed") - return 0 - if self.gbpcfg.gbp_policy_cfg_all( - 2, - 'ruleset', - 'demo_par_prs', - child_policy_rule_sets=child_uuid) == 0: - self._log.info( - "# Step 5: Associating Child PRS by Updating Parent " - "PRS == Failed") - return 0 - self._log.info( - '# Step 5A: Verify Parent and Child Policy RuleSet ' - 'using -show cmd') - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - 'demo_par_prs', - id=parent_uuid, - policy_rules=self.rule_uuid, - shared='False', - child_policy_rule_sets=child_uuid) == 0: - self._log.info( - "# Step 5A: Verify Parent RuleSet using -show " - "option == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - 'demo_child_prs', - id=child_uuid, - policy_rules=self.rule_uuid, - shared='False', - parent_id=parent_uuid) == 0: - self._log.info( - "# Step 5B: Verify Parent RuleSet using -show " - "option == Failed") - return 0 - # Delete Parent PRS and Verify - if self.gbpcfg.gbp_policy_cfg_all(0, 'ruleset', 'demo_par_prs') == 0: - self._log.info("# Step 6: Delete Parent Policy RuleSet == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - 'demo_child_prs', - id=child_uuid, - policy_rules=self.rule_uuid, - shared='False', - parent_id=parent_uuid) != 0: - self._log.info( - "# Step 6A: Verify Child PRS after Delete of " - "Parent PRS using -show option == Failed") - return 0 - self._log.info("\nTESTCASE_GBP_PRS_FUNC_5: PASSED") - return 1 - - def test_gbp_prs_func_6(self): - """ - Changing parent-child prs mapping - Create 4 PRS, two are parent and two are child - Update the one of the parent such that both childs are mapped - Verify - Delete both child PRS - Verify the parent PRS - """ - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PRS_FUNC_6: TO CHANGE/UPDATE/DELETE/VERIFY " - "PARENT and CHILD POLICY RULESET\n" - "TEST_STEP::\n" - "Changing parent-child prs mapping\n" - "Create 4 PRS, two are parent and two are child\n" - "Update the one of the parent such that both childs are mapped\n" - "Verify\n" - "Delete both child PRS\n" - "Verify the parent PRS\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info( - "\n## Step 1: Create 4 Policy RuleSets, 2 Parent & 2 " - "Child with PR ##") - ch_uuids, par_uuids = [], [] - for i in range(1, 3): - child_name, par_name = 'child_%s_prs' % (i), 'par_%s_prs' % (i) - child_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'ruleset', child_name, policy_rules=self.rule_name) - if child_uuid == 0: - self._log.info( - "\n## Step 1: Create Child Policy RuleSet == Failed") - return 0 - parent_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'ruleset', - par_name, - policy_rules=self.rule_name, - child_policy_rule_sets=child_uuid) - if parent_uuid == 0: - self._log.info( - "\n## Step 2: Create Parent Policy RuleSet == Failed") - return 0 - ch_uuids.append(child_uuid) - par_uuids.append(parent_uuid) - # Update One of the Parent with two Child PRSs - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'ruleset', 'par_1_prs', child_policy_rule_sets='"%s %s"' % - (ch_uuids[0], ch_uuids[1])) == 0: - self._log.info( - "\n## Step 3: Update Parent Policy RuleSet == Failed") - return 0 - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'ruleset', 'par_1_prs', 'child_policy_rule_sets', [ - ch_uuids[0], ch_uuids[1]]) == 0: - self._log.info("\n## Step 3A: Child PRS NOT Found in Parent PRS") - return 0 - # Delete Child PRSs - for i in range(2): - if self.gbpcfg.gbp_policy_cfg_all(0, 'ruleset', ch_uuids[i]) == 0: - self._log.info( - "\n## Step 5: Delete of Child PRS child_%s_prs" % - (i)) - return 0 - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'ruleset', 'par_1_prs', 'child_policy_rule_sets', [ - ch_uuids[0], ch_uuids[1]]) != 0: - self._log.info( - "\n## Step 5A: Stale Child PRS Mapping still " - "persists in Parent PRS") - return 0 - self._log.info("\nTESTCASE_GBP_PRS_FUNC_6: PASSED") - return 1 - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_func.py.aim b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_func.py.aim deleted file mode 100755 index 83b644f8b..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_func.py.aim +++ /dev/null @@ -1,637 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import commands -import logging -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs - - -def main(): - # Run the Testcases: - test = test_gbp_prs_func() - if test.test_gbp_prs_func_1() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PRS_FUNC_1') - if test.test_gbp_prs_func_2() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PRS_FUNC_2') - if test.test_gbp_prs_func_3() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PRS_FUNC_3') - if test.test_gbp_prs_func_4() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PRS_FUNC_4') - test._log.info( - "\nTESTCASE_GBP_PRS_FUNC_5: TO CREATE/VERIFY/DELETE/VERIFY " - "a PARENT and CHILD POLICY RULESET\n") - test._log.info("\nTESTCASE_GBP_PRS_FUNC_5: NOT SUPPORTED in ACI") - test._log.info( - "\nTESTCASE_GBP_PRS_FUNC_6: TO CHANGE/UPDATE/DELETE/VERIFY " - "PARENT and CHILD POLICY RULESET\n") - test._log.info("\nTESTCASE_GBP_PRS_FUNC_6: NOT SUPPORTED in ACI") - test.cleanup() - utils_libs.report_results('test_gbp_prs_func', 'test_results.txt') - sys.exit(1) - - -class test_gbp_prs_func(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_prs_func.log' - commands.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_prs_func.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self._log.info( - "\n## START OF GBP POLICY_RULE_SET FUNCTIONALITY TESTSUITE\n") - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.act_name = 'demo_pa' - self.cls_name = 'demo_pc' - self.rule_name = 'demo_pr' - self.ruleset_name = 'demo_prs' - self._log.info('\n## Step 1: Create a PC needed for PRS Testing ##') - self.cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', self.cls_name) - if self.cls_uuid == 0: - self._log.info( - "\nReqd Policy Classifier Create Failed, hence GBP " - "Policy Rule-Set Functional Test Suite Run ABORTED\n") - return - self._log.info('\n## Step 1: Create a PA needed for PRS Testing ##') - self.act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', self.act_name) - if self.act_uuid == 0: - self._log.info( - "\nReqd Policy Action Create Failed, hence GBP " - "Policy Rule-Set Functional Test Suite Run ABORTED\n") - return - self._log.info('\n## Step 1: Create a PR needed for PRS Testing ##') - self.rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'rule', self.rule_name, classifier=self.cls_name, - action=self.act_name) - if self.rule_uuid == 0: - self._log.info( - "\nReqd Policy Rule Create Failed, hence GBP " - "Policy Rule-Set Functional Test Suite Run ABORTED\n ") - return - - def cleanup(self, tc_name=''): - if tc_name != '': - self._log.info('%s: FAILED' % (tc_name)) - for obj in ['ruleset', 'rule', 'classifier', 'action']: - self.gbpcfg.gbp_del_all_anyobj(obj) - - def test_gbp_prs_func_1( - self, - name_uuid='', - ruleset_uuid='', - rep_cr=0, - rep_del=0): - - if rep_cr == 0 and rep_del == 0: - self._log.info( - "\n########################################################\n" - "TESTCASE_GBP_PRS_FUNC_1: TO CREATE/VERIFY/DELETE/VERIFY " - "a POLICY RULESET with DEFAULT ATTRIB VALUE\n" - "TEST_STEP::\n" - "Create Policy RuleSet Object\n" - "Verify the attributes & value, show & list cmds\n" - "Delete Policy RuleSet using Name\n" - "Verify that PR has got deleted, show & list cmds\n" - "##########################################################\n") - - if name_uuid == '': - name_uuid = self.ruleset_name - # Testcase work-flow starts - if rep_cr == 0 or rep_cr == 1: - self._log.info( - '\n## Step 1: Create RuleSet with default attrib vals##\n') - ruleset_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'ruleset', name_uuid) - if ruleset_uuid == 0: - self._log.info("# Step 1: Create RuleSet == Failed") - return 0 - self._log.info('# Step 2A: Verify RuleSet using -list cmd') - if self.gbpverify.gbp_policy_verify_all( - 0, 'ruleset', name_uuid, ruleset_uuid) == 0: - self._log.info( - "# Step 2A: Verify RuleSet using -list option == Failed") - return 0 - self._log.info('# Step 2B: Verify RuleSet using -show cmd') - if self.gbpverify.gbp_policy_verify_all( - 1, 'ruleset', name_uuid, id=ruleset_uuid, - shared='False') == 0: - self._log.info( - "# Step 2B: Verify RuleSet using -show option == Failed") - return 0 - ####### - if rep_del == 0 or rep_del == 1: - self._log.info('\n## Step 3: Delete RuleSet using name ##\n') - if self.gbpcfg.gbp_policy_cfg_all(0, 'ruleset', name_uuid) == 0: - self._log.info("# Step 3: Delete RuleSet == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 0, 'ruleset', name_uuid, ruleset_uuid) != 0: - self._log.info( - "# Step 3A: Verify RuleSet is Deleted using -list " - "option == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, 'ruleset', name_uuid, id=ruleset_uuid, - shared='False') != 0: - self._log.info( - "# Step 3B: Verify RuleSet is Deleted using " - "-show option == Failed") - return 0 - if rep_cr == 0 and rep_del == 0: - self._log.info("\nTESTCASE_GBP_PRS_FUNC_1: PASSED") - return 1 - - def test_gbp_prs_func_2(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PRS_FUNC_2: TO CREATE/VERIFY/DELETE/VERIFY " - "a POLICY RULESET with POLICY RULE\n" - "TEST_STEP::\n" - "Create Policy RuleSet Object with GBP PR\n" - "Verify the attributes & value, show & list cmds\n" - "Delete Policy RuleSet using Name\n" - "Verify that PR has got deleted, show & list cmds\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info("\n## Step 1: Create Policy RuleSet with PR ##") - ruleset_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'ruleset', self.ruleset_name, policy_rules=self.rule_name) - if ruleset_uuid == 0: - self._log.info("# Step 1: Create RuleSet == Failed") - return 0 - self._log.info('# Step 2A: Verify RuleSet using -list cmd') - if self.gbpverify.gbp_policy_verify_all( - 0, 'ruleset', self.ruleset_name, ruleset_uuid) == 0: - self._log.info( - "# Step 2A: Verify RuleSet using -list option == Failed") - return 0 - self._log.info('# Step 2B: Verify RuleSet using -show cmd') - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - self.ruleset_name, - id=ruleset_uuid, - policy_rules=self.rule_uuid, - shared='False') == 0: - self._log.info( - "# Step 2B: Verify RuleSet using -show option == Failed") - return 0 - self.test_gbp_prs_func_1(ruleset_uuid=ruleset_uuid, rep_cr=2) - self._log.info("\nTESTCASE_GBP_PRS_FUNC_2: PASSED") - return 1 - - def test_gbp_prs_func_3(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PRS_FUNC_3: TO UPDATE/VERIFY/DELETE/VERIFY " - "EACH ATTRIB of a POLICY RULESET\n" - "TEST_STEP::\n" - "Create Policy RuleSet using Default param values\n" - "Update Each the Policy Rule's editable params\n" - "Verify the Policy Rule's attributes & values, show & list cmds\n" - "Delete the Policy Rule\n" - "Verify Policy RuleSet successfully deleted\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info('\n## Step 1: Create Policy RuleSet with PR ##\n') - ruleset_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'ruleset', self.ruleset_name, policy_rules=self.rule_name) - if ruleset_uuid != 0: - self._log.info( - "Step 1: Create RuleSet Passed, UUID == %s\n" % - (ruleset_uuid)) - else: - self._log.info("# Step 1: Create RuleSet == Failed") - return 0 - self._log.info('\n## Step 1A: Create new PA ,new PC, new PR##\n') - new_cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', 'grppol_pc1') - if new_cls_uuid == 0: - self._log.info( - "\nNew Classifier Create Failed, hence " - "testcase_gbp_prs_func_3 ABORTED\n") - return 0 - new_act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', 'grppol_pa1') - if new_act_uuid == 0: - self._log.info( - "\nNew Action Create Failed, hence " - "testcase_gbp_prs_func_3 ABORTED\n") - return 0 - new_rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'rule', - 'grppol_pr', - classifier=new_cls_uuid, - action=new_act_uuid, - description="'For devstack demo'") - if new_rule_uuid == 0: - self._log.info( - "\nNew Rule Create Failed, hence " - "testcase_gbp_prs_func_3 ABORTED\n") - return 0 - if self.gbpcfg.gbp_policy_cfg_all( - 2, - 'ruleset', - ruleset_uuid, - name='grppol_prs', - policy_rule=new_rule_uuid, - description="'For devstack demo'") == 0: - self._log.info( - "\nStep 2: Updating Policy RuleSet's Attributes , Failed") - return 0 - # Verify starts - if self.gbpverify.gbp_policy_verify_all( - 0, 'ruleset', 'grppol_prs', ruleset_uuid) == 0: - self._log.info( - "# Step 2A: Verify Policy RuleSet Updated " - "Attributes using -list option == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - ruleset_uuid, - name='grppol_prs', - policy_rules=new_rule_uuid, - description='For devstack demo') == 0: - self._log.info( - "# Step 2B: Verify Policy RuleSet Updated " - "Attributes using -show option == Failed") - return 0 - self._log.info("\nTESTCASE_GBP_PRS_FUNC_3: PASSED") - return 1 - - def test_gbp_prs_func_4(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PRS_FUNC_4: TO CREATE/UPDATE/VERIFY/DELETE/ " - "ASSOCIATING MULTIPLE PRs to 1 POLICY RULESET \n" - "TEST_STEP::\n" - "Create Multiple Policy Rules\n" - "Create Policy RuleSet by associating all the Policy Rules\n" - "Verify that multiple Policy Rules are associated to the " - "Policy RuleSet\n" - "Update the Policy RuleSet such that few Policy Rules " - "are unmapped\n" - "Verify the Policy Rule's attributes & values, show & list cmds\n" - "Update the Policy RuleSet such that all Policy Rules " - "association removed\n" - "Verify the Policy Rule's attributes & values, show & list cmds\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info( - '\n## Step 1A: Create new PA ,new PC, 4 PRs using the ' - 'same PA & PC##\n') - new_cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', 'grppol_pc1') - if new_cls_uuid == 0: - self._log.info( - "\nNew Classifier Create Failed, hence " - "testcase_gbp_prs_func_4 ABORTED\n") - return 0 - new_act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', 'grppol_pa1') - if new_act_uuid == 0: - self._log.info( - "\nNew Action Create Failed, hence " - "testcase_gbp_prs_func_4 ABORTED\n") - return 0 - rule_uuid_list = [] - for i in range(4): - new_rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'rule', - 'grppol_pr_%s' % - (i), - classifier=new_cls_uuid, - action=new_act_uuid, - description="'For devstack demo'") - if new_rule_uuid == 0: - self._log.info( - "\nNew Rule Create Failed, hence " - "testcase_gbp_prs_func_4 ABORTED\n") - return 0 - rule_uuid_list.append(new_rule_uuid) - ruleset_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'ruleset', - 'grppol_prs_many', - policy_rule='"%s %s %s %s"' % - (rule_uuid_list[0], - rule_uuid_list[1], - rule_uuid_list[2], - rule_uuid_list[3]), - description="'For devstack demo'") - if ruleset_uuid == 0: - self._log.info( - "\nStep 2: Updating Policy RuleSet's Attributes , Failed") - return 0 - # Verify starts - if self.gbpverify.gbp_policy_verify_all( - 0, 'ruleset', 'grppol_prs_many', ruleset_uuid) == 0: - self._log.info( - "# Step 2A: Verify Policy RuleSet Updated Attributes " - "using -list option == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - ruleset_uuid, - name='grppol_prs_many', - description='For devstack demo') == 0: - self._log.info( - "# Step 2B: Verify Policy RuleSet Updated Attributes " - "using -show option == Failed") - return 0 - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'ruleset', 'grppol_prs_many', 'policy_rules', - rule_uuid_list) == 0: - self._log.info( - "# Step 2C: Verify Policy RuleSet and its Multiple PRs " - "using -show option == Failed") - return 0 - # Update and Verify the PRS by updating the PRs(removing few existing - # ones) - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'ruleset', 'grppol_prs_many', policy_rule='"%s %s"' % - (rule_uuid_list[0], rule_uuid_list[2])) == 0: - self._log.info( - "# Step 3: Updating Policy RuleSet's Attributes , Failed") - return 0 - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'ruleset', 'grppol_prs_many', 'policy_rules', - rule_uuid_list) != 0: - self._log.info( - "# Step 3A: Verify Policy RuleSet and its Multiple " - "PRs using -show option == Failed") - return 0 - # Update and Verify the PRS by updating the PRs=NULL(unmapping all PRs) - if self.gbpcfg.gbp_policy_cfg_all(2, 'ruleset', 'grppol_prs_many', - policy_rule='""') == 0: - self._log.info( - "# Step 4: Upmapping All Policy Rule from Policy " - "RuleSet , Failed") - return 0 - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'ruleset', 'grppol_prs_many', 'policy_rules', - rule_uuid_list) != 0: - self._log.info( - "# Step 4A: Verify All Policy Rules have been Removed " - "from Policy RuleSet using --show option == Failed") - return 0 - self._log.info("\nTESTCASE_GBP_PRS_FUNC_4: PASSED") - return 1 - - def test_gbp_prs_func_5(self): - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PRS_FUNC_5: TO CREATE/VERIFY/DELETE/VERIFY " - "a PARENT and CHILD POLICY RULESET\n" - "TEST_STEP::\n" - "Create 1 Policy RuleSet using the same PA & PC\n" - "Create the 2nd Policy RuleSet using the same PA, " - "PC and associate PRS-1 as CHILD\n" - "Verify the Child PRS reflect the Parent PRS and viceversa\n" - "Delete the Child PRS\n" - "Verify the Parent PRS has no CHILD\n" - "Create the CHild PRS, associate to the Parent PRS\n" - "Verify the association is established b/e Child and Parent\n" - "Delete the Parent PRS\n" - "Verify the Parent PRS association removed the Child PRS\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info("\n## Step 1: Create Policy RuleSet with PR ##") - child_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'ruleset', 'demo_child_prs', policy_rules=self.rule_name) - if child_uuid == 0: - self._log.info( - "\n## Step 1: Create Child Policy RuleSet == Failed") - return 0 - parent_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'ruleset', - 'demo_par_prs', - policy_rules=self.rule_name, - child_policy_rule_sets=child_uuid) - if parent_uuid == 0: - self._log.info( - "\n## Step 2: Create Parent Policy RuleSet == Failed") - return 0 - self._log.info( - '# Step 2A: Verify Parent and Child Policy RuleSet ' - 'using -show cmd') - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - 'demo_par_prs', - id=parent_uuid, - policy_rules=self.rule_uuid, - shared='False', - child_policy_rule_sets=child_uuid) == 0: - self._log.info( - "\n## Step 2A: Verify Parent RuleSet using -show " - "option == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - 'demo_child_prs', - id=child_uuid, - policy_rules=self.rule_uuid, - shared='False', - parent_id=parent_uuid) == 0: - self._log.info( - "\n## Step 2B: Verify Parent RuleSet using -show " - "option == Failed") - return 0 - # Delete Child PRS - if self.gbpcfg.gbp_policy_cfg_all(0, 'ruleset', 'demo_child_prs') == 0: - self._log.info("# Step 3: Delete Child Policy RuleSet == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - 'demo_par_prs', - id=parent_uuid, - policy_rules=self.rule_uuid, - shared='False', - child_policy_rule_sets=child_uuid) != 0: - self._log.info( - "# Step 3A: Verify Parent PRS after Delete of Child " - "PRS using -show option == Failed") - return 0 - # Create Child PRS,Associate to Parent and Verify - child_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'ruleset', 'demo_child_prs', policy_rules=self.rule_name) - if child_uuid == 0: - self._log.info("# Step 4: Create Child Policy RuleSet == Failed") - return 0 - if self.gbpcfg.gbp_policy_cfg_all( - 2, - 'ruleset', - 'demo_par_prs', - child_policy_rule_sets=child_uuid) == 0: - self._log.info( - "# Step 5: Associating Child PRS by Updating Parent " - "PRS == Failed") - return 0 - self._log.info( - '# Step 5A: Verify Parent and Child Policy RuleSet ' - 'using -show cmd') - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - 'demo_par_prs', - id=parent_uuid, - policy_rules=self.rule_uuid, - shared='False', - child_policy_rule_sets=child_uuid) == 0: - self._log.info( - "# Step 5A: Verify Parent RuleSet using -show " - "option == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - 'demo_child_prs', - id=child_uuid, - policy_rules=self.rule_uuid, - shared='False', - parent_id=parent_uuid) == 0: - self._log.info( - "# Step 5B: Verify Parent RuleSet using -show " - "option == Failed") - return 0 - # Delete Parent PRS and Verify - if self.gbpcfg.gbp_policy_cfg_all(0, 'ruleset', 'demo_par_prs') == 0: - self._log.info("# Step 6: Delete Parent Policy RuleSet == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - 'demo_child_prs', - id=child_uuid, - policy_rules=self.rule_uuid, - shared='False', - parent_id=parent_uuid) != 0: - self._log.info( - "# Step 6A: Verify Child PRS after Delete of " - "Parent PRS using -show option == Failed") - return 0 - self._log.info("\nTESTCASE_GBP_PRS_FUNC_5: PASSED") - return 1 - - def test_gbp_prs_func_6(self): - """ - Changing parent-child prs mapping - Create 4 PRS, two are parent and two are child - Update the one of the parent such that both childs are mapped - Verify - Delete both child PRS - Verify the parent PRS - """ - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PRS_FUNC_6: TO CHANGE/UPDATE/DELETE/VERIFY " - "PARENT and CHILD POLICY RULESET\n" - "TEST_STEP::\n" - "Changing parent-child prs mapping\n" - "Create 4 PRS, two are parent and two are child\n" - "Update the one of the parent such that both childs are mapped\n" - "Verify\n" - "Delete both child PRS\n" - "Verify the parent PRS\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info( - "\n## Step 1: Create 4 Policy RuleSets, 2 Parent & 2 " - "Child with PR ##") - ch_uuids, par_uuids = [], [] - for i in range(1, 3): - child_name, par_name = 'child_%s_prs' % (i), 'par_%s_prs' % (i) - child_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'ruleset', child_name, policy_rules=self.rule_name) - if child_uuid == 0: - self._log.info( - "\n## Step 1: Create Child Policy RuleSet == Failed") - return 0 - parent_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'ruleset', - par_name, - policy_rules=self.rule_name, - child_policy_rule_sets=child_uuid) - if parent_uuid == 0: - self._log.info( - "\n## Step 2: Create Parent Policy RuleSet == Failed") - return 0 - ch_uuids.append(child_uuid) - par_uuids.append(parent_uuid) - # Update One of the Parent with two Child PRSs - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'ruleset', 'par_1_prs', child_policy_rule_sets='"%s %s"' % - (ch_uuids[0], ch_uuids[1])) == 0: - self._log.info( - "\n## Step 3: Update Parent Policy RuleSet == Failed") - return 0 - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'ruleset', 'par_1_prs', 'child_policy_rule_sets', [ - ch_uuids[0], ch_uuids[1]]) == 0: - self._log.info("\n## Step 3A: Child PRS NOT Found in Parent PRS") - return 0 - # Delete Child PRSs - for i in range(2): - if self.gbpcfg.gbp_policy_cfg_all(0, 'ruleset', ch_uuids[i]) == 0: - self._log.info( - "\n## Step 5: Delete of Child PRS child_%s_prs" % - (i)) - return 0 - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'ruleset', 'par_1_prs', 'child_policy_rule_sets', [ - ch_uuids[0], ch_uuids[1]]) != 0: - self._log.info( - "\n## Step 5A: Stale Child PRS Mapping still " - "persists in Parent PRS") - return 0 - self._log.info("\nTESTCASE_GBP_PRS_FUNC_6: PASSED") - return 1 - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_neg.py b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_neg.py deleted file mode 100755 index baa3977fd..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_neg.py +++ /dev/null @@ -1,207 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import subprocess -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs - - -def main(): - - # Run the Testcases: - test = test_gbp_prs_neg() - if test.test_gbp_prs_neg_1() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PRS_NEG_1') - if test.test_gbp_prs_neg_2() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PRS_NEG_2') - if test.test_gbp_prs_neg_3() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PRS_NEG_3') - test.cleanup() - utils_libs.report_results('test_gbp_prs_neg', 'test_results.txt') - sys.exit(1) - - -class test_gbp_prs_neg(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_prs_neg.log' - subprocess.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_prs_neg.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self._log.info( - "\n## START OF GBP POLICY_RULE_SET NEGATIVE TESTSUITE\n") - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.act_name = 'demo_pa' - self.cls_name = 'demo_pc' - self.rule_name = 'demo_pr' - self.ruleset_name = 'demo_prs' - self._log.info('\n## Step 1: Create a PC needed for PRS Testing ##') - self.cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', self.cls_name) - if self.cls_uuid == 0: - self._log.info( - "\nReqd Policy Classifier Create Failed, " - "hence GBP Policy Rule-Set Negative Test Suite Run ABORTED\n") - return - self._log.info('\n## Step 1: Create a PA needed for PRS Testing ##') - self.act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', self.act_name) - if self.act_uuid == 0: - self._log.info( - "\nReqd Policy Action Create Failed, hence GBP " - "Policy Rule-Set Negative Test Suite Run ABORTED\n") - return - self._log.info('\n## Step 1: Create a PR needed for PRS Testing ##') - self.rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'rule', self.rule_name, classifier=self.cls_name, - action=self.act_name) - if self.rule_uuid == 0: - self._log.info( - "\nReqd Policy Rule Create Failed, hence GBP " - "Policy Rule-Set Negative Test Suite Run ABORTED\n ") - return - - def cleanup(self, tc_name=''): - if tc_name != '': - self._log.info('%s: FAILED' % (tc_name)) - for obj in ['ruleset', 'rule', 'classifier', 'action']: - self.gbpcfg.gbp_del_all_anyobj(obj) - - def test_gbp_prs_neg_1(self): - - self._log.info( - "\n#################################################\n" - "TESTCASE_GBP_PRS_NEG_1: TO CREATE/VERIFY POLICY " - "RULESET with INVALID POLICY RULE\n" - "TEST_STEPS::\n" - "Create Policy RuleSet Object with Invalid PR\n" - "Verify the create FAILs and config rolls back\n" - "#################################################\n") - - # Testcase work-flow starts - self._log.info("\n## Step 1: Create Policy RuleSet with Invalid PR ##") - if self.gbpcfg.gbp_policy_cfg_all( - 1, - 'ruleset', - self.ruleset_name, - policy_rules='INVALID') != 0: - self._log.info( - "# Step 1: Create RuleSet with Invalid PR did NOT Fail") - return 0 - self._log.info('# Step 1A: Verify Policy RuleSet config rolled back') - if self.gbpverify.gbp_policy_verify_all( - 1, 'ruleset', self.ruleset_name) != 0: - self._log.info( - "# Step 1A: Verify RuleSet config roll back did NOT Fail") - return 0 - self._log.info("\nTESTCASE_GBP_PRS_NEG_1: PASSED") - return 1 - - def test_gbp_prs_neg_2(self): - - self._log.info( - "\n###################################################\n" - "TESTCASE_GBP_PRS_NEG_2: TO CREATE/VERIFY POLICY " - "RULESET with mix of VALID and INVALID POLICY RULE\n" - "TEST_STEPS::\n" - "Create Policy RuleSet with a mix of Valid and Invalid PR\n" - "Verify the create FAILs and config rolls back\n" - "##################################################\n") - - # Testcase work-flow starts - self._log.info( - "\n## Step 1: Create Policy RuleSet with mix of Valid " - "and Invalid PR ##") - if self.gbpcfg.gbp_policy_cfg_all( - 1, - 'ruleset', - self.ruleset_name, - policy_rules="'%s INVALID'" % - (self.rule_uuid)) != 0: - self._log.info( - "# Step 1: Create RuleSet with mix of Valid and " - "Invalid PR did NOT Fail") - return 0 - self._log.info('# Step 1A: Verify Policy RuleSet config rolled back') - if self.gbpverify.gbp_policy_verify_all( - 1, 'ruleset', self.ruleset_name) != 0: - self._log.info( - "# Step 1A: Verify RuleSet config roll back did NOT Fail") - return 0 - self._log.info("\nTESTCASE_GBP_PRS_NEG_2: PASSED") - return 1 - - def test_gbp_prs_neg_3(self): - - self._log.info( - "\n###################################################\n" - "TESTCASE_GBP_PRS_NEG_3: TO UPDATE/VERIFY POLICY " - "RULE with VALID and INVALID PR\n" - "TEST_STEPS::\n" - "Create a Policy RuleSet with default attribute\n" - "Update the Policy RuleSet with a mix of Valid and Invalid PR\n" - "Verify the update fails and config roll backs to " - "original values of the PRS\n" - "##################################################\n") - - # Testcase work-flow starts - self._log.info('\n## Step 1: Create a PRS with default attribute ##\n') - ruleset_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'ruleset', self.ruleset_name) - if ruleset_uuid == 0: - self._log.info( - "\n## Step 1: Create RuleSet with default attr == Failed") - return 0 - self._log.info( - "\n## Step 2: Update the PRS with VALID PR and INVALID PR") - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'ruleset', ruleset_uuid, policy_rule='"%s INVALID"' % - (self.rule_name)) != 0: - self._log.info( - "\n## Step 2: Updating Policy RuleSet with VALID " - "and INVALID Policy Rules did NOT Fail") - return 0 - self._log.info( - '# Step 2A: Verify RuleSet config update has been rolled back') - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - self.ruleset_name, - id=ruleset_uuid, - policy_rules=self.rule_uuid, - shared='False') != 0: - self._log.info("# Step 2A: Verify RuleSet roll back did NOT Fail") - return 0 - self._log.info("\nTESTCASE_GBP_PRS_NEG_3: PASSED") - return 1 - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_pr_shared_func.py b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_pr_shared_func.py deleted file mode 100755 index 1c447600f..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_pr_shared_func.py +++ /dev/null @@ -1,309 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import subprocess -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs - - -def main(): - # Run the Testcases: - test = test_gbp_prs_pr_shared_func() - if test.test_gbp_prs_pr_shared_func_1() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PRS_PR_SHARED_INTEG_1') - if test.test_gbp_prs_pr_shared_func_2() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PRS_PR_SHARED_INTEG_2') - test.cleanup() - utils_libs.report_results('test_gbp_prs_pr_shared_func', - 'test_results_admin.txt') - sys.exit(1) - - -class test_gbp_prs_pr_shared_func(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_prs_pr_shared_func.log' - subprocess.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_prs_pr_shared_func.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self._log.info( - "\n## START OF GBP POLICY_RULE_SET FUNCTIONALITY TESTSUITE\n") - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.act_name = 'demo_pa' - self.cls_name = 'demo_pc' - self.rule_name = 'demo_pr' - self.ruleset_name = 'demo_prs' - - def cleanup(self, tc_name=''): - if tc_name != '': - self._log.info('%s: FAILED' % (tc_name)) - for obj in ['ruleset', 'rule', 'classifier', 'action']: - self.gbpcfg.gbp_del_all_anyobj(obj) - - def test_gbp_prs_pr_shared_func_1(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PRS_PR_SHARED_INTEG_1: TO " - "CREATE/UPDATE/VERIFY/DELETE/ ASSOCIATING MULTIPLE PRs to 1 " - "POLICY RULESET \n" - "TEST_STEP::\n" - "Create Multiple Policy Rules with shared=True\n" - "Create Policy RuleSet by associating all the Policy " - "Rules and shared=False(default)\n" - "Verify that multiple Policy Rules are associated to " - "the Policy RuleSet\n" - "Update the Policy RuleSet with shared=True\n" - "Verify the Policy RuleSet's shared=True\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info( - '\n## Step 1A: Create new PA ,new PC, 4 PRs using the ' - 'same PA & PC##\n') - new_cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', 'grppol_pc1', shared='True') - if new_cls_uuid == 0: - self._log.info( - "\nNew Classifier Create Failed, hence " - "Testcase_gbp_prs_pr_shared_integ_1 ABORTED\n") - return 0 - new_act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', 'grppol_pa1', shared='True') - if new_act_uuid == 0: - self._log.info( - "\nNew Action Create Failed, hence " - "testcase_gbp_prs_pr_shared_integ_1 ABORTED\n") - return 0 - rule_uuid_list = [] - for i in range(4): - new_rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'rule', - 'grppol_pr_%s' % - (i), - classifier=new_cls_uuid, - action=new_act_uuid, - description="'For devstack demo'", - shared="True") - if new_rule_uuid == 0: - self._log.info( - "\nNew Rule Create Failed, hence " - "testcase_gbp_prs_pr_shared_integ_1 ABORTED\n") - return 0 - rule_uuid_list.append(new_rule_uuid) - ruleset_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'ruleset', - 'grppol_prs_many', - policy_rule='"%s %s %s %s"' % - (rule_uuid_list[0], - rule_uuid_list[1], - rule_uuid_list[2], - rule_uuid_list[3]), - description="'For devstack demo'", - shared='False') - if ruleset_uuid == 0: - self._log.info( - "\nStep 2: Creating Policy RuleSet with multiple " - "PRs(shared=True) and shared=False , Failed") - return 0 - # Verify starts - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - ruleset_uuid, - name='grppol_prs_many', - description='For devstack demo', - shared='False') == 0: - self._log.info( - "# Step 2B: Verify Policy RuleSet and its " - "shared='False' == Failed") - return 0 - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'ruleset', 'grppol_prs_many', 'policy_rules', - rule_uuid_list) == 0: - self._log.info( - "# Step 2C: Verify Policy RuleSet and its " - "Multiple PRs using -show option == Failed") - return 0 - # Update the Policy RuleSet with shared=True and - # update MUST fail as it is not supported by RMD - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'ruleset', 'grppol_prs_many', shared='True') != 0: - self._log.info( - "# Step 3: Updating Policy RuleSet's Attribute " - "shared=True DID NOT Fail") - return 0 - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'ruleset', 'grppol_prs_many', 'policy_rules', - rule_uuid_list) == 0: - self._log.info( - "# Step 3A: Verify Policy RuleSet and its " - "Multiple PRs, == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - ruleset_uuid, - name='grppol_prs_many', - description='For devstack demo', - shared='False') == 0: - self._log.info( - "# Step 3B: Verify Policy RuleSet and its " - "shared=False, == Failed") - return 0 - self._log.info("\nTESTCASE_GBP_PRS_PR_SHARED_INTEG_1: PASSED") - self.cleanup() - return 1 - - def test_gbp_prs_pr_shared_func_2(self): - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PRS_PR_SHARED_INTEG_2: TO " - "CREATE/UPDATE/VERIFY/DELETE/ ASSOCIATING MULTIPLE " - "PRs to 1 POLICY RULESET\n" - "TEST_STEP::\n" - "Create Multiple Policy Rules witha mix of shared=True " - "and shared=False\n" - "Create Policy RuleSet by associating all the Policy " - "Rules and shared=False(default)\n" - "Verify that multiple Policy Rules are associated to " - "the Policy RuleSet\n" - "Update the Policy RuleSet with shared=True and update " - "should fail\n" - "Verify the Policy RuleSet's continues with attribute " - "shared=False\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info( - '\n## Step 1A: Create new PA ,new PC, 4 PRs using the ' - 'same PA & PC##\n') - new_cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', 'grppol_pc1', shared='True') - if new_cls_uuid == 0: - self._log.info( - "\nNew Classifier Create Failed, hence " - "testcase_gbp_prs_pr_shared_integ_2 ABORTED\n") - return 0 - new_act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', 'grppol_pa1', shared='True') - if new_act_uuid == 0: - self._log.info( - "\nNew Action Create Failed, hence " - "testcase_gbp_prs_pr_shared_integ_2 ABORTED\n") - return 0 - rule_uuid_list = [] - shared_flag = ['True', 'False', 'True', 'False'] - for i in range(4): - new_rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'rule', - 'grppol_pr_%s' % - (i), - classifier=new_cls_uuid, - action=new_act_uuid, - description="'For devstack demo'", - shared=shared_flag[i]) - if new_rule_uuid == 0: - self._log.info( - "\nNew Rule Create Failed, hence " - "testcase_gbp_prs_pr_shared_integ_2 ABORTED\n") - return 0 - rule_uuid_list.append(new_rule_uuid) - ruleset_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'ruleset', - 'grppol_prs_many', - policy_rule='"%s %s %s %s"' % - (rule_uuid_list[0], - rule_uuid_list[1], - rule_uuid_list[2], - rule_uuid_list[3]), - description="'For devstack demo'", - shared='False') - if ruleset_uuid == 0: - self._log.info( - "\nStep 2: Creating Policy RuleSet with multiple " - "PRs(shared=True) and shared=False , Failed") - return 0 - # Verify starts - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - ruleset_uuid, - name='grppol_prs_many', - description='For devstack demo', - shared='False') == 0: - self._log.info( - "# Step 2B: Verify Policy RuleSet and its " - "shared='False' == Failed") - return 0 - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'ruleset', 'grppol_prs_many', 'policy_rules', - rule_uuid_list) == 0: - self._log.info( - "# Step 2C: Verify Policy RuleSet and its " - "Multiple PRs using -show option == Failed") - return 0 - # Update and Verify the PRS by updating the PRs(removing few existing - # ones) - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'ruleset', 'grppol_prs_many', shared='True') != 0: - self._log.info( - "# Step 3: Updating Policy RuleSet's" - " Attribute shared=True did NOT fail") - return 0 - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'ruleset', 'grppol_prs_many', 'policy_rules', - rule_uuid_list) == 0: - self._log.info( - "# Step 3A: Verify Policy RuleSet and its " - "Multiple PRs using -show option == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - ruleset_uuid, - name='grppol_prs_many', - description='For devstack demo', - shared='False') == 0: - self._log.info( - "# Step 3B: Verify Policy RuleSet and its " - "shared=False, == Failed") - return 0 - - self._log.info("\nTESTCASE_GBP_PRS_PR_SHARED_INTEG_2: PASSED") - return 1 - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_pr_shared_func.py.aim b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_pr_shared_func.py.aim deleted file mode 100755 index c1e8ed418..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_prs_pr_shared_func.py.aim +++ /dev/null @@ -1,289 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import commands -import logging -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs - - -def main(): - # Run the Testcases: - test = test_gbp_prs_pr_shared_func() - if test.test_gbp_prs_pr_shared_func_1() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PRS_PR_SHARED_INTEG_1') - if test.test_gbp_prs_pr_shared_func_2() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PRS_PR_SHARED_INTEG_2') - test.cleanup() - utils_libs.report_results('test_gbp_prs_pr_shared_func', - 'test_results_admin.txt') - sys.exit(1) - - -class test_gbp_prs_pr_shared_func(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_prs_pr_shared_func.log' - commands.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_prs_pr_shared_func.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self._log.info( - "\n## START OF GBP POLICY_RULE_SET FUNCTIONALITY TESTSUITE\n") - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.act_name = 'demo_pa' - self.cls_name = 'demo_pc' - self.rule_name = 'demo_pr' - self.ruleset_name = 'demo_prs' - - def cleanup(self, tc_name=''): - if tc_name != '': - self._log.info('%s: FAILED' % (tc_name)) - for obj in ['ruleset', 'rule', 'classifier', 'action']: - self.gbpcfg.gbp_del_all_anyobj(obj) - - def test_gbp_prs_pr_shared_func_1(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PRS_PR_SHARED_INTEG_1: TO " - "CREATE/UPDATE/VERIFY/DELETE/ ASSOCIATING MULTIPLE PRs to 1 " - "POLICY RULESET \n" - "TEST_STEP::\n" - "Create Multiple Policy Rules with shared=True\n" - "Create Policy RuleSet by associating all the Policy " - "Rules and shared=False(default)\n" - "Verify that multiple Policy Rules are associated to " - "the Policy RuleSet\n" - "Update the Policy RuleSet with shared=True\n" - "Verify the Policy RuleSet's shared=True\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info( - '\n## Step 1A: Create new PA ,new PC, 4 PRs using the ' - 'same PA & PC##\n') - new_cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', 'grppol_pc1', shared='True') - if new_cls_uuid == 0: - self._log.info( - "\nNew Classifier Create Failed, hence " - "Testcase_gbp_prs_pr_shared_integ_1 ABORTED\n") - return 0 - new_act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', 'grppol_pa1', shared='True') - if new_act_uuid == 0: - self._log.info( - "\nNew Action Create Failed, hence " - "testcase_gbp_prs_pr_shared_integ_1 ABORTED\n") - return 0 - rule_uuid_list = [] - for i in range(4): - new_rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'rule', - 'grppol_pr_%s' % - (i), - classifier=new_cls_uuid, - action=new_act_uuid, - description="'For devstack demo'", - shared="True") - if new_rule_uuid == 0: - self._log.info( - "\nNew Rule Create Failed, hence " - "testcase_gbp_prs_pr_shared_integ_1 ABORTED\n") - return 0 - rule_uuid_list.append(new_rule_uuid) - ruleset_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'ruleset', - 'grppol_prs_many', - policy_rule='"%s %s %s %s"' % - (rule_uuid_list[0], - rule_uuid_list[1], - rule_uuid_list[2], - rule_uuid_list[3]), - description="'For devstack demo'", - shared='False') - if ruleset_uuid == 0: - self._log.info( - "\nStep 2: Creating Policy RuleSet with multiple " - "PRs(shared=True) and shared=False , Failed") - return 0 - # Verify starts - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - ruleset_uuid, - name='grppol_prs_many', - description='For devstack demo', - shared='False') == 0: - self._log.info( - "# Step 2B: Verify Policy RuleSet and its " - "shared='False' == Failed") - return 0 - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'ruleset', 'grppol_prs_many', 'policy_rules', - rule_uuid_list) == 0: - self._log.info( - "# Step 2C: Verify Policy RuleSet and its " - "Multiple PRs using -show option == Failed") - return 0 - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'ruleset', 'grppol_prs_many', shared='True') == 0: - self._log.info( - "# Step 3: Updating Policy RuleSet's Attribute " - "shared=True Failed") - return 0 - self._log.info("\nTESTCASE_GBP_PRS_PR_SHARED_INTEG_1: PASSED") - self.cleanup() - return 1 - - def test_gbp_prs_pr_shared_func_2(self): - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PRS_PR_SHARED_INTEG_2: TO " - "CREATE/UPDATE/VERIFY/DELETE/ ASSOCIATING MULTIPLE " - "PRs to 1 POLICY RULESET\n" - "TEST_STEP::\n" - "Create Multiple Policy Rules witha mix of shared=True " - "and shared=False\n" - "Create Policy RuleSet by associating all the Policy " - "Rules and shared=False(default)\n" - "Verify that multiple Policy Rules are associated to " - "the Policy RuleSet\n" - "Update the Policy RuleSet with shared=True and update " - "should fail\n" - "Verify the Policy RuleSet's continues with attribute " - "shared=False\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info( - '\n## Step 1A: Create new PA ,new PC, 4 PRs using the ' - 'same PA & PC##\n') - new_cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', 'grppol_pc1', shared='True') - if new_cls_uuid == 0: - self._log.info( - "\nNew Classifier Create Failed, hence " - "testcase_gbp_prs_pr_shared_integ_2 ABORTED\n") - return 0 - new_act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', 'grppol_pa1', shared='True') - if new_act_uuid == 0: - self._log.info( - "\nNew Action Create Failed, hence " - "testcase_gbp_prs_pr_shared_integ_2 ABORTED\n") - return 0 - rule_uuid_list = [] - shared_flag = ['True', 'False', 'True', 'False'] - for i in range(4): - new_rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'rule', - 'grppol_pr_%s' % - (i), - classifier=new_cls_uuid, - action=new_act_uuid, - description="'For devstack demo'", - shared=shared_flag[i]) - if new_rule_uuid == 0: - self._log.info( - "\nNew Rule Create Failed, hence " - "testcase_gbp_prs_pr_shared_integ_2 ABORTED\n") - return 0 - rule_uuid_list.append(new_rule_uuid) - ruleset_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'ruleset', - 'grppol_prs_many', - policy_rule='"%s %s %s %s"' % - (rule_uuid_list[0], - rule_uuid_list[1], - rule_uuid_list[2], - rule_uuid_list[3]), - description="'For devstack demo'", - shared='False') - if ruleset_uuid == 0: - self._log.info( - "\nStep 2: Creating Policy RuleSet with multiple " - "PRs(shared=True) and shared=False , Failed") - return 0 - # Verify starts - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - ruleset_uuid, - name='grppol_prs_many', - description='For devstack demo', - shared='False') == 0: - self._log.info( - "# Step 2B: Verify Policy RuleSet and its " - "shared='False' == Failed") - return 0 - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'ruleset', 'grppol_prs_many', 'policy_rules', - rule_uuid_list) == 0: - self._log.info( - "# Step 2C: Verify Policy RuleSet and its " - "Multiple PRs using -show option == Failed") - return 0 - # Update and Verify the PRS by updating the PRs(removing few existing - # ones) - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'ruleset', 'grppol_prs_many', shared='True') != 0: - self._log.info( - "# Step 3: Updating Policy RuleSet's" - " Attribute shared=True did NOT fail") - return 0 - if self.gbpverify.gbp_obj_ver_attr_all_values( - 'ruleset', 'grppol_prs_many', 'policy_rules', - rule_uuid_list) == 0: - self._log.info( - "# Step 3A: Verify Policy RuleSet and its " - "Multiple PRs using -show option == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'ruleset', - ruleset_uuid, - name='grppol_prs_many', - description='For devstack demo', - shared='False') == 0: - self._log.info( - "# Step 3B: Verify Policy RuleSet and its " - "shared=False, == Failed") - return 0 - - self._log.info("\nTESTCASE_GBP_PRS_PR_SHARED_INTEG_2: PASSED") - return 1 - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ptg_func.py b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ptg_func.py deleted file mode 100755 index f183d605d..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ptg_func.py +++ /dev/null @@ -1,456 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import subprocess -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs - - -def main(): - - # Run the Testcases: - env_flag = sys.argv[1] - test = test_gbp_ptg_func(env_flag) - test.global_cfg() - if test.test_gbp_ptg_func_1() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PTG_FUNC_1') - test.global_cfg() # Making global_cfg available for the subsequent TC - if test.test_gbp_ptg_func_2() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PTG_FUNC_2') - test.global_cfg() - if test.test_gbp_ptg_func_3() == 0: - test.cleanup(tc_name='TESTCASE_GBP_PTG_FUNC_3') - test.cleanup() - utils_libs.report_results('test_gbp_ptg_func', 'test_results.txt') - sys.exit(1) - - -class test_gbp_ptg_func(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - hdlr = logging.FileHandler('/tmp/test_gbp_ptg_func.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self, env_flag): - """ - Init def - """ - self._log.info( - "\n## START OF GBP POLICY_TARGET_GROUP FUNCTIONALITY TESTSUITE\n") - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.act_name = 'test_ptg_pa' - self.cls_name = 'test_ptg_pc' - self.rule_name = 'test_ptg_pr' - self.ruleset_name = 'test_ptg_prs' - self.ptg_name = 'demo_ptg' - self.l2p_name = 'test_ptg_l2p' - self.l3p_name = 'test_ptg_l3p' - self.pt_name = 'test_pt' - self.env_flag = env_flag - if self.env_flag == 'aci': - self.def_ip_pool = '192.168.0.0/16' - self.cidr = '192.168.0.0/24' - else: - self.def_ip_pool = '10.0.0.0/8' - self.cidr = '10.0.0.0/24' - - def global_cfg(self): - self._log.info('\n## Step 1: Create a PC needed for PTG Testing ##') - self.cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', self.cls_name) - if self.cls_uuid == 0: - self._log.info( - "\nReqd Policy Classifier Create Failed, hence GBP " - "Policy Target-Group Functional Test Suite Run ABORTED\n") - return 0 - self._log.info('\n## Step 1: Create a PA needed for PTG Testing ##') - self.act_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'action', self.act_name) - if self.act_uuid == 0: - self._log.info( - "\n## Reqd Policy Action Create Failed, hence GBP " - "Policy Target-Group Functional Test Suite Run ABORTED\n") - return 0 - self._log.info('\n## Step 1: Create a PR needed for PTG Testing ##') - self.rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'rule', self.rule_name, classifier=self.cls_name, - action=self.act_name) - if self.rule_uuid == 0: - self._log.info( - "\n## Reqd Policy Rule Create Failed, hence GBP Policy " - "Target-Group Functional Test Suite Run ABORTED\n ") - return 0 - self._log.info('\n## Step 1: Create a PRS needed for PTG Testing ##') - self.prs_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'ruleset', self.ruleset_name, policy_rules=self.rule_name) - if self.prs_uuid == 0: - self._log.info( - "\n## Reqd Policy Target-Group Create Failed, hence " - "GBP Policy Target-Group Functional Test Suite " - "Run ABORTED\n ") - return 0 - l3p_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'l3p', self.l3p_name, ip_pool='20.20.0.0/24', - subnet_prefix_length='28', _proxy_ip_pool='20.20.1.0/24', - _proxy_subnet_prefix_length='28') - if l3p_uuid == 0: - self._log.info( - "\n## Reqd L3Policy Create Failed, hence GBP Policy " - "Target-Group Functional Test Suite Run ABORTED\n") - return 0 - self.gbpcfg.gbp_policy_cfg_all( - 1, 'l2p', self.l2p_name, l3_policy=l3p_uuid) - - def cleanup(self, tc_name=''): - if tc_name != '': - self._log.info('%s: FAILED' % (tc_name)) - for obj in [ - 'target', - 'group', - 'l2p', - 'l3p', - 'ruleset', - 'rule', - 'classifier', - 'action']: - self.gbpcfg.gbp_del_all_anyobj(obj) - - def test_gbp_ptg_func_1( - self, - name_uuid='', - ptg_uuid='', - rep_cr=0, - rep_del=0): - - if rep_cr == 0 and rep_del == 0: - self._log.info( - "\n########################################################\n" - "TESTCASE_GBP_PTG_FUNC_1: TO CREATE/VERIFY/DELETE/VERIFY a " - "POLICY TARGET-GROUP with DEFAULT ATTRIB VALUE\n" - "TEST_STEP::\n" - "Create Policy Target-Group Object\n" - "Verify the attributes & value, show & list cmds\n" - "Verify the implicitly GBP(L2P,L3P) & " - "Neutron(net,subnet,dhcp-port) Objects\n" - "Delete Policy Target-Group using Name\n" - "Verify the PTG has got deleted, show & list cmds\n" - "Verify the implicit GBP & Neutron Objects are deleted\n" - "##########################################################\n") - - if name_uuid == '': - name_uuid = self.ptg_name - # Testcase work-flow starts - if rep_cr == 0 or rep_cr == 1: - self._log.info( - '\n## Step 1: Create Target-Group with default ' - 'attrib vals##\n') - uuids = self.gbpcfg.gbp_policy_cfg_all(1, 'group', name_uuid) - if uuids != 0: - ptg_uuid = uuids[0] - l2pid = uuids[1] - subnetid = uuids[2] - else: - self._log.info("\n## Step 1: Create Target-Group == Failed") - return 0 - self._log.info('\n## Step 2A: Verify Target-Group using -list cmd') - if self.gbpverify.gbp_policy_verify_all( - 0, 'group', name_uuid, ptg_uuid) == 0: - self._log.info( - "\n## Step 2A: Verify Target-Group using -list " - "option == Failed") - return 0 - self._log.info('\n## Step 2B: Verify Target-Group using -show cmd') - if self.gbpverify.gbp_policy_verify_all( - 1, 'group', name_uuid, id=ptg_uuid, shared='False') == 0: - self._log.info( - "\n## Step 2B: Verify Target-Group using -show " - "option == Failed") - return 0 - # Verify the implicit objects(gbp & neutron) - ret_uuid = self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, 'l2p', self.ptg_name, ret='default', id=l2pid, - policy_target_groups=ptg_uuid) - if ret_uuid != 0 and len(ret_uuid) == 2: - l3pid = ret_uuid[0] - ntkid = ret_uuid[1] - else: - self._log.info( - "\n## Step 2C: Verify By-Default L2Policy == Failed") - return 0 - rtr_uuid = self.gbpverify.gbp_l2l3ntk_pol_ver_all( - 1, - 'l3p', - l3pid, - ret='default', - id=l3pid, - name='default', - ip_pool=self.def_ip_pool, - l2_policies=l2pid, - subnet_prefix_length='24', - ip_version='4') - if rtr_uuid != 0 and isinstance(rtr_uuid, str) == 0: - self._log.info( - "# Step 2D: Verify By-Default L3Policy == Failed") - return 0 - net_name = 'l2p_%s' % (name_uuid) - if self.gbpverify.neut_ver_all( - 'net', - ntkid, - name=net_name, - admin_state_up='True', - subnets=subnetid) == 0: - self._log.info( - "# Step 2E: Implicit-creation of Neutron Network-Obj " - "-show option == Failed") - return 0 - if self.gbpverify.neut_ver_all( - 'subnet', - subnetid, - cidr=self.cidr, - enable_dhcp='True', - network_id=ntkid) == 0: - self._log.info( - "\n## Step 2F: Implicit-creation of Neutron SubNet-Obj " - "== Failed") - return 0 - if self.env_flag != 'aci': - if self.gbpverify.neut_ver_all( - 'router', - rtr_uuid, - admin_state_up='True', - status='ACTIVE') == 0: - self._log.info( - "\n## Step 2G: Implicit-creation of Neutron " - "Router-Obj == Failed") - return 0 - # Delete and Verify - if rep_del == 0 or rep_del > 0: - self._log.info('\n## Step 3: Delete Target-Group using name ##\n') - if self.gbpcfg.gbp_policy_cfg_all(0, 'group', ptg_uuid) == 0: - self._log.info("\n## Step 3: Delete Target-Group == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all(0, 'group', ptg_uuid) != 0: - self._log.info( - "\n## Step 3A: Verify Target-Group is Deleted " - "using -list option == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all(1, 'group', ptg_uuid) != 0: - self._log.info( - "\n## Step 3B: Verify Target-Group is Deleted " - "using -show option == Failed") - return 0 - if rep_cr == 0 and rep_del == 0: - self._log.info("\n## TESTCASE_GBP_PTG_FUNC_1: PASSED") - return 1 - - def test_gbp_ptg_func_2(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PTG_FUNC_2: TO CREATE/VERIFY/DELETE/VERIFY " - "a POLICY TARGET-GROUP with POLICY RULESET\n" - "TEST_STEPS::\n" - "Create Policy Target-Group Object with ConsumedPRS=A\n" - "Verify the attributes & value, show & list cmds\n" - "Update the PTG's atribute ProvidedPRS=A\n" - "Create a PRS=B\n" - "Update the PTG's attributes Consumed & Provided PRS=B\n" - "Delete Policy Target-Group using Name\n" - "Verify that Target-Group has got deleted, show & list cmds\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info("\n## Step 1: Create Policy Target-Group with PRS ##") - uuids = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'group', - self.ptg_name, - consumed_policy_rule_sets='%s=scope' % - (self.ruleset_name)) - if uuids != 0: - ptg_uuid = uuids[0].rstrip() - subnetid = uuids[2].rstrip() - else: - self._log.info("\n## Step 1: Create Target-Group == Failed") - return 0 - self._log.info( - '\n## Step 2A: Verify Policy Target-Group using -list cmd') - if self.gbpverify.gbp_policy_verify_all( - 0, 'group', self.ptg_name, ptg_uuid) == 0: - self._log.info( - "\n## Step 2A: Verify Target-Group using -list " - "option == Failed") - return 0 - self._log.info( - '\n## Step 2B: Verify Policy Target-Group using -show cmd') - if self.gbpverify.gbp_policy_verify_all( - 1, - 'group', - self.ptg_name, - id=ptg_uuid, - shared='False', - subnets=subnetid, - consumed_policy_rule_sets=self.prs_uuid) == 0: - self._log.info( - "\n## Step 2B: Verify Policy Target-Group using -show " - "option == Failed") - return 0 - # Update the PTG's Provided PRS - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'group', ptg_uuid, provided_policy_rule_sets='%s=scope' % - (self.ruleset_name), name='ptg_new') == 0: - self._log.info( - "\n## Step 3: Updating Policy Target-Group == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'group', - 'ptg_new', - id=ptg_uuid, - shared='False', - subnets=subnetid, - consumed_policy_rule_sets=self.prs_uuid, - provided_policy_rule_sets=self.prs_uuid) == 0: - self._log.info( - "\n## Step 3A: Verify after updating Policy " - "Target-Group == Failed") - return 0 - # Create new PRS and update both Provided & Consumed PRS attrs - new_prs_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'ruleset', 'demo-new-prs', policy_rules=self.rule_name) - if new_prs_uuid == 0: - self._log.info( - "\n## Step 4: Reqd Policy Target-Group Create Failed, " - "hence Testcase_gbp_ptg_func_2 Run ABORTED\n ") - return 0 - if self.gbpcfg.gbp_policy_cfg_all( - 2, - 'group', - ptg_uuid, - provided_policy_rule_sets='demo-new-prs=scope', - consumed_policy_rule_sets='demo-new-prs=scope') == 0: - self._log.info( - "\n## Step 5: Updating Policy Target-Group with new " - "PRS == Failed") - return 0 - if self.gbpverify.gbp_policy_verify_all( - 1, - 'group', - 'ptg_new', - id=ptg_uuid, - shared='False', - subnets=subnetid, - consumed_policy_rule_sets=new_prs_uuid, - provided_policy_rule_sets=new_prs_uuid) == 0: - self._log.info( - "\n## Step 5A: Verify after updating Policy " - "Target-Group == Failed") - return 0 - # Delete the PTG and verify - self.test_gbp_ptg_func_1(ptg_uuid=ptg_uuid, rep_del=2, rep_cr=2) - self._log.info("\n## TESTCASE_GBP_PTG_FUNC_2: PASSED") - return 1 - - def test_gbp_ptg_func_3(self): - - self._log.info( - "\n############################################################\n" - "TESTCASE_GBP_PTG_FUNC_3: TO UPDATE A POLICY " - "TARGET-GROUP AFTER DELETING PT's NEUTRON PORT \n" - "TEST_STEPS::\n" - "Create Policy Target-Group using L2P and NO PRS\n" - "Create a Policy Target using the above Policy-Target-Group\n" - "Delete the neutron port corresponding to the Policy-Target\n" - "Update the Policy-Target-Group with a PRS\n" - "Verify Policy Target-Group successfully updated\n" - "##############################################################\n") - - # Testcase work-flow starts - self._log.info('\n## Step 1: Create Policy Target-Group with L2P ##\n') - uuids = self.gbpcfg.gbp_policy_cfg_all( - 1, 'group', self.ptg_name, l2_policy=self.l2p_name) - if uuids != 0: - ptg_uuid = uuids[0] - else: - self._log.info("\n## Step 1: Create Target-Group == Failed") - return 0 - self._log.info( - '\n## Step 2: Create a Policy Target using the above ' - 'Policy-Target-Group\n') - uuids = self.gbpcfg.gbp_policy_cfg_all( - 1, 'target', self.pt_name, policy_target_group=ptg_uuid) - if uuids != 0: - pt_uuid = uuids[0] - neutron_port_id = uuids[1] - else: - self._log.info("\n## Step 2: Create Policy Target == Failed") - return 0 - self._log.info( - '\n## Step 2A: Verify the Implicit creation of Neutron Port\n') - if self.gbpverify.neut_ver_all('port', neutron_port_id) == 0: - self._log.info( - "\n## Step 2A: Implicit creation neutron port-object " - "== Failed") - return 0 - self._log.info( - '\n## Step 3: Delete the neutron port corresponding to the ' - 'Policy-Target\n') - cmd = 'neutron port-delete %s' % (neutron_port_id) - if self.gbpcfg.cmd_error_check(subprocess.getoutput(cmd)) == 0: - self._log.info( - "\n## Step 3: Deletion of the neutron port corresponding " - "to the Policy-Target = Failed") - return 0 - self._log.info( - '\n## Step 4: Update the Policy-Target-Group with a PRS\n') - if self.gbpcfg.gbp_policy_cfg_all( - 2, 'group', ptg_uuid, provided_policy_rule_sets='%s=scope' % - (self.prs_uuid), consumed_policy_rule_sets='%s=scope' % - (self.prs_uuid)) == 0: - self._log.info( - "\n## Step 4: Updating Policy Target-Group with " - "new PRS == Failed") - return 0 - self._log.info( - '\n## Step 5: Verify Policy Target-Group successfully updated\n') - if self.gbpverify.gbp_policy_verify_all( - 1, - 'group', - self.ptg_name, - id=ptg_uuid, - shared='False', - policy_targets=pt_uuid, - consumed_policy_rule_sets=self.prs_uuid, - provided_policy_rule_sets=self.prs_uuid) == 0: - self._log.info( - "\n## Step 5A: Verify after updating Policy " - "Target-Group == Failed") - return 0 - self._log.info("\n## TESTCASE_GBP_PTG_FUNC_3: PASSED") - return 1 - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ri_func_1.py b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ri_func_1.py deleted file mode 100755 index 9e94d79ff..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ri_func_1.py +++ /dev/null @@ -1,188 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import subprocess -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs - - -def main(): - - # Run the Testcase: - test = test_gbp_ri_func_1() - test.run() - - -class test_gbp_ri_func_1(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_ri_func_1.log' - subprocess.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_ri_func_1.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.act_name = 'allow_all' - self.class_name = 'pc_icmp' - self.rule_name = 'pr_icmp' - self.ruleset_name = 'prs_icmp' - self.ptg_name = 'pg_icmp' - self.tg_name = 'tg_icmp' - - def cleanup(self, cfgobj, uuid_name, fail=0): - if isinstance(cfgobj, str): - cfgobj = [cfgobj] - if isinstance(uuid_name, str): - uuid_name = [uuid_name] - for obj, _id in zip(cfgobj, uuid_name): - if self.gbpcfg.gbp_policy_cfg_all(0, obj, _id): - self._log.info( - 'Success in Clean-up/Delete of Policy Object %s\n' % - (obj)) - else: - self._log.info( - 'Failed to Clean-up/Delete of Policy Object %s\n' % - (obj)) - if fail != 0: - self._log.info("\n## TESTCASE_GBP_RI_FUNC_1: FAILED") - utils_libs.report_results('test_gbp_ri_func_1', 'test_results.txt') - sys.exit(1) - - def run(self): - self._log.info( - "\n## TESTCASE_GBP_RI_FUNC_1: RESOURCE INTEGRITY AMONG " - "GBP's PA,PC,PR,PRS,PTG,PT OBJs") - # Testcase work-flow starts - # ============ ALL POLICY OBJECTS ARE TO BE CREATED AND VERIFIED = - self._log.info("\n## Step 1: Create Action ##\n") - act_uuid = self.gbpcfg.gbp_action_config(1, self.act_name) - if act_uuid == 0: - self._log.info("# Step 1: Create Action == Failed") - self.cleanup('action', act_uuid, fail=1) - - self._log.info("\n## Step 2: Create Classifier ##\n") - cls_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'classifier', self.class_name, protocol='icmp', direction='bi') - # this is needed for cleanup,can append and sort for the sake of - # order... but it kept it simple - objs, names = ['classifier', 'action'], [cls_uuid, act_uuid] - if cls_uuid == 0: - self._log.info("# Step 2: Create Classifier == Failed") - self.cleanup(objs, names, fail=1) - ###### - self._log.info("\n## Step 3: Create Policy Rule ##\n") - rule_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'rule', self.rule_name, classifier=self.class_name, - action=self.act_name) - objs, names = [ - 'rule', 'classifier', 'action'], [ - rule_uuid, cls_uuid, act_uuid] - if rule_uuid == 0: - self._log.info("# Step 3: Create Policy Rule == Failed") - self.cleanup(objs, names, fail=1) - self._log.info( - "\n## Step 4: Delete in-use Policy Action & Classifier ##\n") - if self.gbpcfg.gbp_policy_cfg_all( - 0, 'classifier', self.class_name) != 0: - self._log.info( - "\n# Step 4A: Delete in-use Policy Classifier did not fail #") - self.cleanup(objs, names, fail=1) - if self.gbpcfg.gbp_action_config(0, self.act_name) != 0: - self._log.info( - "\n# Step 4B: Delete in-use Policy Action did not fail #") - self.cleanup(objs, names, fail=1) - ###### - self._log.info("\n## Step 5: Create Policy Rule-Set ##\n") - ruleset_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'ruleset', self.ruleset_name, policy_rules=self.rule_name) - objs, names = ['ruleset', 'rule', 'classifier', 'action'],\ - [ruleset_uuid, rule_uuid, cls_uuid, act_uuid] - if ruleset_uuid == 0: - self._log.info("# Step 5: Create Policy Rule-Set == Failed") - self.cleanup(objs, names, fail=1) - - self._log.info("\n## Step 5A: Delete of in-use Policy Rule ##\n") - if self.gbpcfg.gbp_policy_cfg_all(0, 'rule', self.rule_name) != 0: - self._log.info( - "\n# Step 5A: Delete in-use Policy Rule did not fail") - self.cleanup(objs, names, fail=1) - ##### - self._log.info("\n## Step 7: Create Policy Target-Grp ##\n") - uuids = self.gbpcfg.gbp_policy_cfg_all( - 1, - 'group', - self.ptg_name, - consumed_policy_rule_sets='%s=scope' % - (self.ruleset_name)) - if uuids != 0: - ptg_uuid = uuids[0].rstrip() - objs, names = ( - ['group', 'ruleset', 'rule', 'classifier', 'action'], - [ptg_uuid, ruleset_uuid, rule_uuid, cls_uuid, act_uuid]) - else: - self._log.info("# Step 7: Create Policy Target-Grp == Failed") - self.cleanup(objs, names, fail=1) - - self._log.info("\n## Step 7A: Delete in-use Policy RuleSet ##\n") - if self.gbpcfg.gbp_policy_cfg_all( - 0, 'ruleset', self.ruleset_name) != 0: - self._log.info( - "\n# Step 7A: Delete in-use Policy RuleSet did not fail") - self.cleanup(objs, names, fail=1) - - self._log.info("\n## Step 8: Create Policy Targets ##\n") - ret_uuids = self.gbpcfg.gbp_policy_cfg_all( - 1, 'target', self.tg_name, policy_target_group=self.ptg_name) - if ret_uuids != 0 and len(ret_uuids) == 2: - pt_uuid = ret_uuids[0] - objs, names = ( - ['target', 'group', 'ruleset', 'rule', 'classifier', 'action'], - [pt_uuid, ptg_uuid, ruleset_uuid, rule_uuid, cls_uuid, - act_uuid]) - self._log.info( - "# Step 8: Creation of Policy Target Passed, UUID == %s\n" % - (pt_uuid)) - else: - self._log.info("# Step 8: Creation of Policy Target == Failed") - self.cleanup(objs, names, fail=1) - - self._log.info("\n## Step 8: Delete in-use Policy Target Group ##\n") - if self.gbpcfg.gbp_policy_cfg_all( - 0, 'ruleset', self.ruleset_name) != 0: - self._log.info( - "\n# Step 8A: Delete in-use Policy RuleSet did not fail") - self.cleanup(objs, names, fail=1) - self._log.info("\n## TESTCASE_GBP_RI_FUNC_1: PASSED") - self.cleanup(objs, names) # Cleanup the system - utils_libs.report_results('test_gbp_ri_func_1', 'test_results.txt') - sys.exit(1) - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ri_func_2.py b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ri_func_2.py deleted file mode 100755 index fe4e751fc..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ri_func_2.py +++ /dev/null @@ -1,145 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import subprocess -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs - - -def main(): - - # Run the Testcase: - test = test_gbp_ri_func_2() - test.run() - - -class test_gbp_ri_func_2(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_ri_func_2.log' - subprocess.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_ri_func_2.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.l3pol_name = 'demo_l3pol' - self.l2pol_name = 'demo_l2pol' - self.ptg_name = 'demo_ptg' - - def cleanup(self, cfgobj, uuid_name, tc_name='', fail=0): - if isinstance(cfgobj, str): - cfgobj = [cfgobj] - if isinstance(uuid_name, str): - uuid_name = [uuid_name] - for obj, _id in zip(cfgobj, uuid_name): - if self.gbpcfg.gbp_policy_cfg_all(0, obj, _id): - self._log.info( - 'Success in Clean-up/Delete of Policy Object %s\n' % - (obj)) - else: - self._log.info( - 'Failed to Clean-up/Delete of Policy Object %s\n' % - (obj)) - if fail != 0: - self._log.info("\n## TESTCASE_GBP_RI_FUNC_2: FAILED") - subprocess.report_results('test_gbp_ri_func_2', 'test_results.txt') - sys.exit(1) - - def run(self): - self._log.info( - "\n## TESTCASE_GBP_RI_FUNC_2A: RESOURCE INTEGRITY AMONG " - "L2POLICY and L3POLICY OBJs") - # Testcase work-flow starts - # ============ ALL POLICY OBJECTS ARE TO BE CREATED AND VERIFIED = - self._log.info("\n## Step 1: Create L3Policy ##\n") - l3p_uuid = self.gbpcfg.gbp_policy_cfg_all(1, 'l3p', self.l3pol_name) - if l3p_uuid == 0: - self._log.info("# Step 1: Create L3Policy == Failed") - self.cleanup( - 'l3p', - l3p_uuid, - tc_name='TESTCASE_GBP_RI_FUNC_2A', - fail=1) - ###### - self._log.info("\n## Step 2: Create L2Policy using L3Policy ##\n") - l2p_uuid = self.gbpcfg.gbp_policy_cfg_all( - 1, 'l2p', self.l2pol_name, l3_policy=l3p_uuid) - # this is needed for cleanup,can append and sort for the sake - # of order.. but it kept it simple. l2p_uuid[1] is same as l3p_uuid, - # just that cfg_all func returns both uuid when l2p is obj - objs, names = ['l2p', 'l3p'], [l2p_uuid[0], l2p_uuid[1]] - if l2p_uuid == 0: - self._log.info("# Step 2: Create L2Policy == Failed") - self.cleanup(objs, names, fail=1) - self._log.info("\n## Step 3: Delete in-use L3Policy ##\n") - if self.gbpcfg.gbp_policy_cfg_all(0, 'l3p', l3p_uuid) != 0: - self._log.info( - "\n# Step 4A: Delete in-use L3Policy did not fail #") - self.cleanup( - objs, - names, - tc_name='TESTCASE_GBP_RI_FUNC_2A', - fail=1) - else: - self._log.info("\n## TESTCASE_GBP_RI_FUNC_2A: PASSED") - ###### - self._log.info( - "\n## TESTCASE_GBP_RI_FUNC_2B: RESOURCE INTEGRITY AMONG L2POLICY " - "and PTG OBJs") - self._log.info("\n## Step 5: Create Policy Target-Grp ##\n") - uuids = self.gbpcfg.gbp_policy_cfg_all( - 1, 'group', self.ptg_name, l2_policy=l2p_uuid[0]) - if uuids != 0: - objs, names = ['group', 'l2p', 'l3p'],\ - [self.ptg_name, l2p_uuid[0], l3p_uuid] - else: - self._log.info("# Step 5: Create Policy Target-Grp == Failed") - self.cleanup( - objs, - names, - tc_name='TESTCASE_GBP_RI_FUNC_2B', - fail=1) - - self._log.info("\n## Step 5A: Delete in-use L2 Policy ##") - if self.gbpcfg.gbp_policy_cfg_all(0, 'l2p', l2p_uuid[0]) != 0: - self._log.info("\n# Step 5A: Delete in-use L2Policy did not fail") - self.cleanup( - objs, - names, - tc_name='TESTCASE_GBP_RI_FUNC_2B', - fail=1) - else: - self._log.info("\n## TESTCASE_GBP_RI_FUNC_2B: PASSED") - self.cleanup(objs, names) # Cleanup the system now - utils_libs.report_results('test_gbp_ri_func_2', 'test_results.txt') - sys.exit(1) - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ri_func_3.py b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ri_func_3.py deleted file mode 100755 index 9491895ec..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ri_func_3.py +++ /dev/null @@ -1,118 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import subprocess -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs - - -def main(): - - # Run the Testcase: - test = test_gbp_ri_func_3() - test.run() - - -class test_gbp_ri_func_3(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_ri_func_3.log' - subprocess.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_ri_func_3.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.act_name = 'demo_act' - self.spec_name = 'demo_sc_spec' - - def cleanup(self, cfgobj, uuid_name, fail=0): - if isinstance(cfgobj, str): - cfgobj = [cfgobj] - if isinstance(uuid_name, str): - uuid_name = [uuid_name] - for obj, _id in zip(cfgobj, uuid_name): - if obj == 'action': - if self.gbpcfg.gbp_policy_cfg_all(0, obj, _id) == 0: - self._log.info( - 'Failed to Clean-up/Delete of Policy Object %s\n' % - (obj)) - else: - if self.gbpcfg.gbp_sc_cfg_all(0, obj, _id) == 0: - self._log.info( - 'Failed to Clean-up/Delete of Policy Object %s\n' % - (obj)) - if fail != 0: - self._log.info("\n## TESTCASE_GBP_RI_FUNC_3: FAILED") - utils_libs.report_results('test_gbp_ri_func_3', 'test_results.txt') - sys.exit(1) - - def run(self): - self._log.info( - "\n## TESTCASE_GBP_RI_FUNC_3: RESOURCE INTEGRITY " - "AMONG POLICY ACTION and SC OBJs") - # Testcase work-flow starts - # ============ ALL POLICY OBJECTS ARE TO BE CREATED AND VERIFIED = - self._log.info( - "\n## Step 1: Create Policy Action with type Redirect ##\n") - act_uuid = self.gbpcfg.gbp_action_config(1, self.act_name) - if act_uuid == 0: - self._log.info("# Step 1: Create Action == Failed") - self.cleanup('action', act_uuid, fail=1) - ###### - self._log.info("\n## Step 2: Create ServiceChain Spec ##\n") - spec_uuid = self.gbpcfg.gbp_sc_cfg_all(1, 'spec', self.spec_name) - # this is needed for cleanup,can append and sort for the sake of order. - # but it kept it simple - objs, names = ['action', 'spec'], [act_uuid, spec_uuid] - if spec_uuid == 0: - self._log.info("# Step 2: Create Classifier == Failed") - self.cleanup(objs, names, fail=1) - ###### - self._log.info( - "\n## Step 3: Update the Policy Action with SCSpec ##\n") - if self.gbpcfg.gbp_action_config( - 2, act_uuid, action_value=spec_uuid) == 0: - self._log.info( - "\n##Step 2: Updating Policy Action's Attributes name & " - "action_value == Failed") - self.cleanup(objs, names, fail=1) - ###### - self._log.info("\n## Step 4: Delete ServiceChain Spec ##\n") - if self.gbpcfg.gbp_sc_cfg_all(0, 'spec', spec_uuid) != 0: - self._log.info("# Step 4: Deletion of ServiceChain did NOT fail") - self.cleanup(objs, names, fail=1) - else: - self._log.info("\n## TESTCASE_GBP_RI_FUNC_3: PASSED") - self.cleanup(objs, names) - utils_libs.report_results('test_gbp_ri_func_3', 'test_results.txt') - sys.exit(1) - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ri_func_4.py b/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ri_func_4.py deleted file mode 100755 index ea62c84d5..000000000 --- a/gbpservice/tests/contrib/gbpfunctests/testcases/tc_gbp_ri_func_4.py +++ /dev/null @@ -1,109 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import logging -import subprocess -import sys - -from libs import config_libs -from libs import utils_libs -from libs import verify_libs - - -def main(): - - # Run the Testcase: - test = test_gbp_ri_func_4() - test.run() - - -class test_gbp_ri_func_4(object): - - # Initialize logging - logging.basicConfig( - format='%(asctime)s [%(levelname)s] %(name)s - %(message)s', - level=logging.WARNING) - _log = logging.getLogger(__name__) - cmd = 'rm /tmp/test_gbp_ri_func_4.log' - subprocess.getoutput(cmd) - hdlr = logging.FileHandler('/tmp/test_gbp_ri_func_4.log') - formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') - hdlr.setFormatter(formatter) - _log.addHandler(hdlr) - _log.setLevel(logging.INFO) - _log.setLevel(logging.DEBUG) - - def __init__(self): - """ - Init def - """ - self.gbpcfg = config_libs.Gbp_Config() - self.gbpverify = verify_libs.Gbp_Verify() - self.spec_name = 'demo_sc_spec' - self.fw_name = 'demo_fw' - self.lb_name = 'demo_lb' - - def cleanup(self, fail=0): - for obj in ['node', 'spec']: - self.gbpcfg.gbp_del_all_anyobj(obj) - if fail != 0: - self._log.info("\n## TESTCASE_GBP_RI_FUNC_4: FAILED") - utils_libs.report_results('test_gbp_ri_func_4', 'test_results.txt') - sys.exit(1) - - def run(self): - self._log.info( - "\n## TESTCASE_GBP_RI_FUNC_4: RESOURCE INTEGRITY AMONG " - "SERVICE-CHAIN OBJECTS") - # Testcase work-flow starts - # ============ ALL POLICY OBJECTS ARE TO BE CREATED AND VERIFIED = - self._log.info("\n## Step 1: Create Service Chain Nodes LB & FW ##\n") - lb_uuid = self.gbpcfg.gbp_sc_cfg_all(1, 'node', self.lb_name) - if lb_uuid == 0: - self._log.info( - "# Step 1: Create Service Chain Loadbalance Node == Failed") - self.cleanup(fail=1) - fw_uuid = self.gbpcfg.gbp_sc_cfg_all(1, 'node', self.lb_name) - if fw_uuid == 0: - self._log.info( - "# Step 1A: Create Service Chain Firewall Node == Failed") - self.cleanup(fail=1) - ###### - self._log.info("\n## Step 2: Create ServiceChain Spec ##\n") - # Ensur that node names or node uuids passed as val to param - # 'nodes',MUST be in order of FW and then LB.. this order is required - # from gbp pov - spec_uuid = self.gbpcfg.gbp_sc_cfg_all( - 1, 'spec', self.spec_name, nodes='%s %s' % - (fw_uuid, lb_uuid)) - if spec_uuid == 0: - self._log.info("# Step 2: Create ServiceChain Spec == Failed") - self.cleanup(fail=1) - ###### - self._log.info("\n## Step 3: Delete the Service Chain Nodes ##\n") - cnt = 0 - for nodeid in [lb_uuid, fw_uuid]: - if self.gbpcfg.gbp_sc_cfg_all(0, 'node', nodeid) != 0: - self._log.info( - "# Step 4: Deletion of ServiceChain did NOT fail") - cnt += 1 - if cnt > 0: - self.cleanup(fail=1) - else: - self._log.info("\n## TESTCASE_GBP_RI_FUNC_4: PASSED") - self.cleanup() - utils_libs.report_results('test_gbp_ri_func_4', 'test_results.txt') - sys.exit(1) - - -if __name__ == '__main__': - main() diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/test_results.txt b/gbpservice/tests/contrib/gbpfunctests/testcases/test_results.txt deleted file mode 100755 index e69de29bb..000000000 diff --git a/gbpservice/tests/contrib/gbpfunctests/testcases/test_results_admin.txt b/gbpservice/tests/contrib/gbpfunctests/testcases/test_results_admin.txt deleted file mode 100755 index e69de29bb..000000000 diff --git a/gbpservice/tests/contrib/post_test_hook.sh b/gbpservice/tests/contrib/post_test_hook.sh deleted file mode 100644 index 39b56c139..000000000 --- a/gbpservice/tests/contrib/post_test_hook.sh +++ /dev/null @@ -1,47 +0,0 @@ -#!/bin/bash - -source functions-gbp - -set -x - -trap prepare_logs ERR - -sudo git --git-dir=/opt/stack/new/neutron/.git --work-tree=/opt/stack/new/neutron show --name-only -sudo git --git-dir=/opt/stack/new/neutron/.git --work-tree=/opt/stack/new/neutron status -sudo pip show neutron-lib -sudo git --git-dir=/opt/stack/new/group-based-policy/.git --work-tree=/opt/stack/new/group-based-policy show --name-only -sudo git --git-dir=/opt/stack/new/group-based-policy/.git --work-tree=/opt/stack/new/group-based-policy status - -# The following is added to get past the error: -# "publicURL endpoint for compute service in RegionOne region not found" -# when trying to launch a VM in the exercise scripts -sudo /bin/systemctl restart memcached - -sudo chown -R stack:stack $TOP_DIR - -# Run exercise scripts -run_exercises -exercises_exit_code=$? - -# Check if exercises left any resources undeleted -check_residual_resources admin admin -check_residual_resources admin demo -check_residual_resources demo demo - -# Run gbpfunc integration tests -echo "Running gbpfunc test suite" -export PYTHONPATH="$GBP_FUNC_DIR:${PYTHONPATH}" -cd $GBP_FUNC_DIR/testcases -# Run tests as non-admin cred -source_creds $TOP_DIR/openrc demo demo -python suite_non_admin_run.py upstream -gbpfunc_non_admin_exit_code=$? -# Run shared_resource tests as admin cred -source_creds $TOP_DIR/openrc admin admin -python suite_admin_run.py -gbpfunc_admin_exit_code=$? - -# Prepare the log files for Jenkins to upload -prepare_logs - -exit $(($exercises_exit_code+$gbpfunc_admin_exit_code+$gbpfunc_non_admin_exit_code)) diff --git a/gbpservice/tests/contrib/post_test_nfp_hook.sh b/gbpservice/tests/contrib/post_test_nfp_hook.sh deleted file mode 100644 index 392ed39c6..000000000 --- a/gbpservice/tests/contrib/post_test_nfp_hook.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/bash - -source functions-gbp - -set -x - -trap prepare_logs ERR - -# Run exercise scripts -$TOP_DIR/exercise.sh -exercises_exit_code=$? - -source $TOP_DIR/lib/nfp -delete_nfp_gbp_resources $TOP_DIR - -# Check if exercises left any resources undeleted -check_residual_resources neutron service -check_residual_resources admin admin -check_residual_resources admin demo -check_residual_resources demo demo - -# Prepare the log files for Jenkins to upload -prepare_logs - -exit $(($exercises_exit_code)) diff --git a/gbpservice/tests/contrib/post_test_rally_hook.sh b/gbpservice/tests/contrib/post_test_rally_hook.sh deleted file mode 100644 index 0b04eca65..000000000 --- a/gbpservice/tests/contrib/post_test_rally_hook.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash - -source functions-gbp - -set -x - -trap prepare_logs ERR - -run_gbp_rally -exit_code=$? - -# Prepare the log files for Jenkins to upload -prepare_logs - -exit $exit_code