def retrieve_topology_elements(service_id, iResult): agent = get_host_agent() topology_edge_stub = get_stub(agent.current_controller, agent.controller_port, topology_edge_rpc_service) vswitch_interface_stub = get_stub(agent.current_controller, agent.controller_port, vswitch_interface_rpc_service) vswitch_lanzone_stub = get_stub(agent.current_controller, agent.controller_port, vswitch_lanzone_rpc_service) vswitch_host_stub = get_stub(agent.current_controller, agent.controller_port, vswitch_host_rpc_service) e3loger.info('applying service: %s' % (service_id)) _edges = rpc_client_list_topology_edges_for_services(topology_edge_stub, [service_id]) edges = {_edge.id : _edge for _edge in _edges} _iface_list = set() _ifaces = list() for _edge_id in edges: _edge = edges[_edge_id] _iface_list.add(_edge.interface0) _iface_list.add(_edge.interface1) if len(_iface_list): _ifaces = rpc_client_list_vswitch_interfaces(vswitch_interface_stub, _iface_list) ifaces = {_iface.id : _iface for _iface in _ifaces} _lanzone_list = set() _host_list = set() _lanzones = list() _hosts = list() for _iface_id in ifaces: _iface = ifaces[_iface_id] _lanzone_list.add(_iface.lanzone_id) _host_list.add(_iface.host_id) if len(_lanzone_list): _lanzones = rpc_client_list_vswitch_lanzones(vswitch_lanzone_stub, uuid_list = _lanzone_list) lanzones = {_lanzone.id : _lanzone for _lanzone in _lanzones} if len(_host_list): _hosts = rpc_client_list_vswitch_hosts(vswitch_host_stub, uuid_list = _host_list) hosts = {_host.id : _host for _host in _hosts} iResult['edges'] = edges iResult['interfaces'] = ifaces iResult['lanzones'] = lanzones iResult['hosts'] = hosts iResult['service_id'] = service_id iResult['service'] = db_update_ether_service(service_id)
def db_update_ether_service(service_id): agent = get_host_agent() ether_service_stub = get_stub(agent.current_controller, agent.controller_port, ether_service_rpc_service) session = db_sessions[DB_NAME]() try: ether_service_guard.write_lock() session.begin() local_service = session.query(agent_ether_service).filter( agent_ether_service.id == service_id).first() if local_service: return local_service.clone() local_service = agent_ether_service() shadow_service = rpc_client_get_ether_service(ether_service_stub, service_id) local_service.id = shadow_service.id local_service.name = shadow_service.name local_service.service_type = shadow_service.service_type local_service.tenant_id = shadow_service.tenant_id local_service.created_at = shadow_service.created_at local_service.link_type = shadow_service.link_type session.add(local_service) session.commit() return local_service.clone() finally: session.close() ether_service_guard.write_unlock()
def _push_ether_lan_topology_on_creation(config, iResult): host_grpc_port = get_config(None, 'grpc', 'host_grpc_port') host_to_push = iResult['host_to_push'] e_lan = invt_get_vswitch_ether_service(iResult['service_id']) for _host_id in host_to_push: _host = invt_get_vswitch_host(_host_id) stub = get_stub(_host.host_ip, host_grpc_port, rpc_service) rpc_client_push_ether_services(stub, [e_lan])
def _delete_ether_line_services(config, iResult): host_grpc_port = get_config(None, 'grpc', 'host_grpc_port') invt_interfaces = invt_list_vswitch_interfaces() invt_edges = invt_list_vswitch_topology_edges() service_ids = config['service_ids'] hosts_to_push = dict() #collect the services to push to hosts for _service_id in service_ids: service = invt_get_vswitch_ether_service(_service_id) edges = [invt_edges[_edge] for _edge in invt_edges if \ invt_edges[_edge].service_id == _service_id] for _edge in edges: iface0 = invt_interfaces[_edge.interface0] iface1 = invt_interfaces[_edge.interface1] host0 = iface0.host_id host1 = iface1.host_id if host0 not in hosts_to_push: hosts_to_push[host0] = dict() if host1 not in hosts_to_push: hosts_to_push[host1] = dict() hosts_to_push[host0][_service_id] = service hosts_to_push[host1][_service_id] = service #delete topology edges before push the services so that the host sync process #know what the applied operation is ['add', 'delete'] for _edge_id in invt_edges: _edge = invt_edges[_edge_id] if _edge.service_id not in service_ids: continue invt_unregister_vswitch_topology_edge(_edge_id) #push services through RPC call for _host_id in hosts_to_push: _host = invt_get_vswitch_host(_host_id) stub = get_stub(_host.host_ip, host_grpc_port, rpc_service) rpc_client_push_ether_services(stub, list(hosts_to_push[_host_id].values())) #delete ether_services for _servie_id in service_ids: invt_unregister_vswitch_ether_service(_servie_id)
spec.ban_lanzones.append(bl) for bi in ban_interfaces: spec.ban_interfaces.append(bi) spec.is_synced = is_synced stub.rpc_taslflow_update_ether_lan_service(spec) publish_stub_inventory(rpc_service, ether_service_pb2_grpc.ether_serviceStub) if __name__ == '__main__': from e3net.common.e3config import add_config_file from e3net.common.e3config import load_configs import sys add_config_file('/etc/e3net/e3vswitch.ini') load_configs() stub = get_stub('127.0.0.1', 9418, rpc_service) if False: rpc_client_taskflow_create_ether_service( stub, service_name='ether_line_0', tenant_id='c2204787-df90-473b-8ed6-52838eab1c32', initial_lanzones=[ '1bcd32a0-444d-41cb-a3fd-786f6f3ef83c', '2ddc6ac9-5216-4e25-8414-9e088c33a94f', 'abe1b66e-7f34-4857-bdb8-ec27b76373a3' ], service_type=E3NET_ETHER_SERVICE_TYPE_LAN, is_synced=True) #rpc_client_taskflow_delete_ether_service(stub, ['f666831e-7e02-4b12-a39f-f0bca96ef6ab'], is_synced = True) rpc_client_taskflow_update_ether_lan_service( stub,
def e3neta_agent_connect(): agent = get_host_agent() if agent.connected == True: return vswitch_host_stub = get_stub(agent.current_controller, agent.controller_port, 'vswitch_host') vswitch_iface_stub = get_stub(agent.current_controller, agent.controller_port, 'vswitch_interface') vswitch_lanzone_stub = get_stub(agent.current_controller, agent.controller_port, 'vswitch_lanzone') vswitch_host = None vswitch_ifaces = None try: vswitch_host = rpc_client_get_vswitch_host(vswitch_host_stub, agent.hostname, False) except grpc.RpcError as e: e3neta_intercept_exception(agent, e) if e.user_error and e.user_exception == E3_EXCEPTION_NOT_FOUND: vswitch_host = rpc_client_register_vswitch_host( vswitch_host_stub, name=agent.hostname, host_ip=agent.local_ip, description=agent.description, host_status=E3VSWITCH_HOST_STATUS_ACTIVE) assert (vswitch_host) if agent.hostname != vswitch_host.name or \ agent.local_ip != vswitch_host.host_ip or \ agent.description != vswitch_host.description or \ vswitch_host.host_status != E3VSWITCH_HOST_STATUS_ACTIVE: vswitch_host = rpc_client_update_vswitch_host( vswitch_host_stub, uuid=vswitch_host.id, name=agent.hostname, host_ip=agent.local_ip, description=agent.description, host_status=E3VSWITCH_HOST_STATUS_ACTIVE) agent.vswitch_host = vswitch_host vswitch_ifaces = rpc_client_list_vswitch_interfaces_for_hosts( vswitch_iface_stub, [vswitch_host.id]) local_devs = { agent.interfaces[_iface].dev_address: agent.interfaces[_iface] for _iface in agent.interfaces } registered_devs = list() for viface in vswitch_ifaces: assert (viface.host_id == vswitch_host.id) if viface.dev_address in local_devs: local_dev = local_devs[viface.dev_address] registered_devs.append(viface.dev_address) lanzone = rpc_client_get_vswitch_lanzone( vswitch_lanzone_stub, local_devs[viface.dev_address].lanzone, False) iface = viface if viface.interface_type != local_dev.iface_type or \ viface.lanzone_id != lanzone.id or \ viface.interface_status != E3VSWITCH_INTERFACE_STATUS_ACTIVE: iface = rpc_client_update_vswitch_interface( vswitch_iface_stub, viface.id, interface_status=E3VSWITCH_INTERFACE_STATUS_ACTIVE, interface_type=local_dev.iface_type, lanzone_id=lanzone.id) for _iface in agent.interfaces: if agent.interfaces[_iface].dev_address == viface.dev_address: agent.interfaces[_iface].vswitch_interface = iface else: #delete the viface if it's not in local-dev list rpc_client_unregister_vswitch_interface(vswitch_iface_stub, viface.id) #register those who are not registered yet for dev in local_devs: if dev in registered_devs: continue lanzone = rpc_client_get_vswitch_lanzone(vswitch_lanzone_stub, local_devs[dev].lanzone, False) iface = rpc_client_register_vswitch_interface( vswitch_iface_stub, host_id=vswitch_host.id, dev_address=dev, lanzone_id=lanzone.id, interface_status=E3VSWITCH_INTERFACE_STATUS_ACTIVE, interface_type=local_devs[dev].iface_type) for _iface in agent.interfaces: if agent.interfaces[_iface].dev_address == dev: agent.interfaces[_iface].vswitch_interface = iface