def setUp(self): self.nsx_cache = sync.NvpCache() for lswitch in LSWITCHES: self.nsx_cache._uuid_dict_mappings[lswitch['uuid']] = ( self.nsx_cache._lswitches) self.nsx_cache._lswitches[lswitch['uuid']] = ({ 'data': lswitch, 'hash': hash(json.dumps(lswitch)) }) for lswitchport in LSWITCHPORTS: self.nsx_cache._uuid_dict_mappings[lswitchport['uuid']] = ( self.nsx_cache._lswitchports) self.nsx_cache._lswitchports[lswitchport['uuid']] = ({ 'data': lswitchport, 'hash': hash(json.dumps(lswitchport)) }) for lrouter in LROUTERS: self.nsx_cache._uuid_dict_mappings[lrouter['uuid']] = ( self.nsx_cache._lrouters) self.nsx_cache._lrouters[lrouter['uuid']] = ({ 'data': lrouter, 'hash': hash(json.dumps(lrouter)) }) super(CacheTestCase, self).setUp()
def _show(self, resource_type, response_file, uuid1, uuid2=None, relations=None): target_uuid = uuid2 or uuid1 if resource_type.endswith('attachment'): resource_type = resource_type[:resource_type.index('attachment')] with open("%s/%s" % (self.fake_files_path, response_file)) as f: response_template = f.read() res_dict = getattr(self, '_fake_%s_dict' % resource_type) for item in res_dict.itervalues(): if 'tags' in item: item['tags_json'] = jsonutils.dumps(item['tags']) # replace sec prof rules with their json dump def jsonify_rules(rule_key): if rule_key in item: rules_json = jsonutils.dumps(item[rule_key]) item['%s_json' % rule_key] = rules_json jsonify_rules('logical_port_egress_rules') jsonify_rules('logical_port_ingress_rules') items = [jsonutils.loads(response_template % res_dict[res_uuid]) for res_uuid in res_dict if res_uuid == target_uuid] if items: return jsonutils.dumps(items[0]) raise api_exc.ResourceNotFound()
def __call__(self, target, creds): """ Check http: rules by calling to a remote server. This example implementation simply verifies that the response is exactly 'True'. """ url = ('http:' + self.match) % target data = {'target': jsonutils.dumps(target), 'credentials': jsonutils.dumps(creds)} post_data = urllib.urlencode(data) f = urllib2.urlopen(url, post_data) return f.read() == "True"
def _lsn_port_configure_action( cluster, lsn_id, lsn_port_id, action, is_enabled, obj): nsxlib.do_request(HTTP_PUT, nsxlib._build_uri_path(LSERVICESNODE_RESOURCE, resource_id=lsn_id, extra_action=action), json.dumps({"enabled": is_enabled}), cluster=cluster) nsxlib.do_request(HTTP_PUT, nsxlib._build_uri_path(LSERVICESNODEPORT_RESOURCE, parent_resource_id=lsn_id, resource_id=lsn_port_id, extra_action=action), json.dumps(obj), cluster=cluster)
def _lsn_port_configure_action(cluster, lsn_id, lsn_port_id, action, is_enabled, obj): nsxlib.do_request(HTTP_PUT, nsxlib._build_uri_path(LSERVICESNODE_RESOURCE, resource_id=lsn_id, extra_action=action), jsonutils.dumps({"enabled": is_enabled}), cluster=cluster) nsxlib.do_request(HTTP_PUT, nsxlib._build_uri_path(LSERVICESNODEPORT_RESOURCE, parent_resource_id=lsn_id, resource_id=lsn_port_id, extra_action=action), jsonutils.dumps(obj), cluster=cluster)
def _pack_json_msg(self, msg): """Qpid cannot serialize dicts containing strings longer than 65535 characters. This function dumps the message content to a JSON string, which Qpid is able to handle. :param msg: May be either a Qpid Message object or a bare dict. :returns: A Qpid Message with its content field JSON encoded. """ try: msg.content = jsonutils.dumps(msg.content) except AttributeError: # Need to have a Qpid message so we can set the content_type. msg = qpid_messaging.Message(jsonutils.dumps(msg)) msg.content_type = JSON_CONTENT_TYPE return msg
def test_create_port(self): path = PORTS_URI % (TEST_TENANT, TEST_NET) with mock.patch.object(self.nvsdlib, 'send_request') as send_request: fixed_ips = [{'ip_address': '10.0.0.2', 'subnet_id': TEST_SUBNET}] lport = { "id": TEST_PORT, "name": 'test', "device_id": "device_id", "device_owner": "device_owner", "mac_address": "mac_address", "fixed_ips": fixed_ips, "admin_state_up": True, "network_id": TEST_NET, "status": 'ACTIVE' } self.nvsdlib.create_port(TEST_TENANT, lport) expected = {"id": TEST_PORT, "name": 'test', "device_id": "device_id", "device_owner": "device_owner", "mac_address": "mac_address", "ip_address": '10.0.0.2', "subnet_id": TEST_SUBNET, "admin_state_up": True, "network_id": TEST_NET, "status": 'ACTIVE'} send_request.assert_called_once_with("POST", path, body=json.dumps(expected), resource='port', tenant_id=TEST_TENANT)
def format(self, record): message = { "message": record.getMessage(), "asctime": self.formatTime(record, self.datefmt), "name": record.name, "msg": record.msg, "args": record.args, "levelname": record.levelname, "levelno": record.levelno, "pathname": record.pathname, "filename": record.filename, "module": record.module, "lineno": record.lineno, "funcname": record.funcName, "created": record.created, "msecs": record.msecs, "relative_created": record.relativeCreated, "thread": record.thread, "thread_name": record.threadName, "process_name": record.processName, "process": record.process, "traceback": None, } if hasattr(record, "extra"): message["extra"] = record.extra if record.exc_info: message["traceback"] = self.formatException(record.exc_info) return jsonutils.dumps(message)
def serialize_msg(raw_msg): # NOTE(russellb) See the docstring for _RPC_ENVELOPE_VERSION for more # information about this format. msg = {_VERSION_KEY: _RPC_ENVELOPE_VERSION, _MESSAGE_KEY: jsonutils.dumps(raw_msg)} return msg
def __init__(self, conf, session, node_name, node_opts=None): """Init the Publisher class with the exchange_name, routing_key, and other options """ self.sender = None self.session = session if conf.qpid_topology_version == 1: addr_opts = { "create": "always", "node": { "type": "topic", "x-declare": { "durable": False, # auto-delete isn't implemented for exchanges in qpid, # but put in here anyway "auto-delete": True, }, }, } if node_opts: addr_opts["node"]["x-declare"].update(node_opts) self.address = "%s ; %s" % (node_name, jsonutils.dumps(addr_opts)) elif conf.qpid_topology_version == 2: self.address = node_name else: raise_invalid_topology_version() self.reconnect(session)
def update_router_lport(cluster, lrouter_uuid, lrouter_port_uuid, tenant_id, neutron_port_id, display_name, admin_status_enabled, ip_addresses): """Updates a logical port on the assigned logical router.""" lport_obj = dict(admin_status_enabled=admin_status_enabled, display_name=display_name, tags=utils.get_tags(os_tid=tenant_id, q_port_id=neutron_port_id), ip_addresses=ip_addresses, type="LogicalRouterPortConfig") # Do not pass null items to NSX for key in lport_obj.keys(): if lport_obj[key] is None: del lport_obj[key] path = _build_uri_path(LROUTERPORT_RESOURCE, lrouter_port_uuid, parent_resource_id=lrouter_uuid) result = do_request(HTTP_PUT, path, jsonutils.dumps(lport_obj), cluster=cluster) LOG.debug( _("Updated logical port %(lport_uuid)s on " "logical router %(lrouter_uuid)s"), { 'lport_uuid': lrouter_port_uuid, 'lrouter_uuid': lrouter_uuid }) return result
def create_router_lport(cluster, lrouter_uuid, tenant_id, neutron_port_id, display_name, admin_status_enabled, ip_addresses, mac_address=None): """Creates a logical port on the assigned logical router.""" lport_obj = dict( admin_status_enabled=admin_status_enabled, display_name=display_name, tags=utils.get_tags(os_tid=tenant_id, q_port_id=neutron_port_id), ip_addresses=ip_addresses, type="LogicalRouterPortConfig" ) # Only add the mac_address to lport_obj if present. This is because # when creating the fake_ext_gw there is no mac_address present. if mac_address: lport_obj['mac_address'] = mac_address path = _build_uri_path(LROUTERPORT_RESOURCE, parent_resource_id=lrouter_uuid) result = do_request(HTTP_POST, path, jsonutils.dumps(lport_obj), cluster=cluster) LOG.debug(_("Created logical port %(lport_uuid)s on " "logical router %(lrouter_uuid)s"), {'lport_uuid': result['uuid'], 'lrouter_uuid': lrouter_uuid}) return result
def _goose_handler(req, res): #NOTE: This only handles JSON responses. # You can use content type header to test for XML. data = jsonutils.loads(res.body) data['FOXNSOX:googoose'] = req.GET.get('chewing') res.body = jsonutils.dumps(data) return res
def _bands_handler(req, res): #NOTE: This only handles JSON responses. # You can use content type header to test for XML. data = jsonutils.loads(res.body) data['FOXNSOX:big_bands'] = 'Pig Bands!' res.body = jsonutils.dumps(data) return res
def deploy_edge(self, request): if (self._unique_router_name and not self._validate_edge_name(request['name'])): header = { 'status': 400 } msg = ('Edge name should be unique for tenant. Edge %s ' 'already exists for default tenant.') % request['name'] response = { 'details': msg, 'errorCode': 10085, 'rootCauseString': None, 'moduleName': 'vShield Edge', 'errorData': None } return (header, jsonutils.dumps(response)) self._job_idx = self._job_idx + 1 job_id = "jobdata-%d" % self._job_idx self._edge_idx = self._edge_idx + 1 edge_id = "edge-%d" % self._edge_idx self._jobs[job_id] = edge_id self._edges[edge_id] = { 'name': request['name'], 'request': request, 'nat_rules': None, 'nat_rule_id': 0 } header = { 'status': 200, 'location': 'https://host/api/4.0/jobs/%s' % job_id } response = '' return (header, response)
def create_lswitch(cluster, neutron_net_id, tenant_id, display_name, transport_zones_config, shared=None, **kwargs): # The tag scope adopts a slightly different naming convention for # historical reasons lswitch_obj = { "display_name": utils.check_and_truncate(display_name), "transport_zones": transport_zones_config, "replication_mode": cfg.CONF.NSX.replication_mode, "tags": utils.get_tags(os_tid=tenant_id, quantum_net_id=neutron_net_id) } # TODO(salv-orlando): Now that we have async status synchronization # this tag is perhaps not needed anymore if shared: lswitch_obj["tags"].append({"tag": "true", "scope": "shared"}) if "tags" in kwargs: lswitch_obj["tags"].extend(kwargs["tags"]) uri = nsxlib._build_uri_path(LSWITCH_RESOURCE) lswitch = nsxlib.do_request(HTTP_POST, uri, jsonutils.dumps(lswitch_obj), cluster=cluster) LOG.debug(_("Created logical switch: %s"), lswitch['uuid']) return lswitch
def plug_interface(cluster, lswitch_id, lport_id, att_obj): return nsxlib.do_request(HTTP_PUT, nsxlib._build_uri_path(LSWITCHPORT_RESOURCE, lport_id, lswitch_id, is_attachment=True), json.dumps(att_obj), cluster=cluster)
def create_lport(cluster, lswitch_uuid, tenant_id, neutron_port_id, display_name, device_id, admin_status_enabled, mac_address=None, fixed_ips=None, port_security_enabled=None, security_profiles=None, queue_id=None, mac_learning_enabled=None, allowed_address_pairs=None): """Creates a logical port on the assigned logical switch.""" display_name = utils.check_and_truncate(display_name) lport_obj = dict( admin_status_enabled=admin_status_enabled, display_name=display_name, tags=utils.get_tags(os_tid=tenant_id, q_port_id=neutron_port_id, vm_id=utils.device_id_to_vm_id(device_id)) ) _configure_extensions(lport_obj, mac_address, fixed_ips, port_security_enabled, security_profiles, queue_id, mac_learning_enabled, allowed_address_pairs) path = nsxlib._build_uri_path(LSWITCHPORT_RESOURCE, parent_resource_id=lswitch_uuid) result = nsxlib.do_request(HTTP_POST, path, json.dumps(lport_obj), cluster=cluster) LOG.debug(_("Created logical port %(result)s on logical switch %(uuid)s"), {'result': result['uuid'], 'uuid': lswitch_uuid}) return result
def update_port(cluster, lswitch_uuid, lport_uuid, neutron_port_id, tenant_id, display_name, device_id, admin_status_enabled, mac_address=None, fixed_ips=None, port_security_enabled=None, security_profiles=None, queue_id=None, mac_learning_enabled=None, allowed_address_pairs=None): lport_obj = dict( admin_status_enabled=admin_status_enabled, display_name=utils.check_and_truncate(display_name), tags=utils.get_tags(os_tid=tenant_id, q_port_id=neutron_port_id, vm_id=utils.device_id_to_vm_id(device_id))) _configure_extensions(lport_obj, mac_address, fixed_ips, port_security_enabled, security_profiles, queue_id, mac_learning_enabled, allowed_address_pairs) path = "/ws.v1/lswitch/" + lswitch_uuid + "/lport/" + lport_uuid try: result = nsxlib.do_request(HTTP_PUT, path, json.dumps(lport_obj), cluster=cluster) LOG.debug(_("Updated logical port %(result)s " "on logical switch %(uuid)s"), {'result': result['uuid'], 'uuid': lswitch_uuid}) return result except exception.NotFound as e: LOG.error(_("Port or Network not found, Error: %s"), str(e)) raise exception.PortNotFoundOnNetwork( port_id=lport_uuid, net_id=lswitch_uuid)
def create_or_update_agent(self, context, agent): """Create or update agent according to report.""" with context.session.begin(subtransactions=True): res_keys = ['agent_type', 'binary', 'host', 'topic'] res = dict((k, agent[k]) for k in res_keys) configurations_dict = agent.get('configurations', {}) res['configurations'] = jsonutils.dumps(configurations_dict) current_time = timeutils.utcnow() try: agent_db = self._get_agent_by_type_and_host( context, agent['agent_type'], agent['host']) res['heartbeat_timestamp'] = current_time if agent.get('start_flag'): res['started_at'] = current_time greenthread.sleep(0) agent_db.update(res) except ext_agent.AgentNotFoundByTypeHost: greenthread.sleep(0) res['created_at'] = current_time res['started_at'] = current_time res['heartbeat_timestamp'] = current_time res['admin_state_up'] = True agent_db = Agent(**res) greenthread.sleep(0) context.session.add(agent_db) greenthread.sleep(0)
def cfg_sync_routers(self, context, host, router_ids=None, hosting_device_ids=None): """Sync routers according to filters to a specific Cisco cfg agent. @param context: contains user information @param host - originator of callback @param router_ids - list of router ids to return information about @param hosting_device_ids - list of hosting device ids to get routers for. @return: a list of routers with their hosting devices, interfaces and floating_ips """ context = neutron_context.get_admin_context() try: routers = ( self._l3plugin.list_active_sync_routers_on_hosting_devices( context, host, router_ids, hosting_device_ids)) except AttributeError: routers = [] if routers and utils.is_extension_supported( self._core_plugin, constants.PORT_BINDING_EXT_ALIAS): self._ensure_host_set_on_ports(context, host, routers) LOG.debug('Routers returned to Cisco cfg agent@%(agt)s:\n %(routers)s', {'agt': host, 'routers': jsonutils.dumps(routers, indent=5)}) return routers
def create_port(self, tenant_id, port): network_id = port["network_id"] fixed_ips = port.get("fixed_ips") ip_address = None subnet_id = None if fixed_ips: ip_address = fixed_ips[0].get("ip_address") subnet_id = fixed_ips[0].get("subnet_id") lport = { "id": port["id"], "name": port["name"], "device_id": port["device_id"], "device_owner": port["device_owner"], "mac_address": port["mac_address"], "ip_address": ip_address, "subnet_id": subnet_id, "admin_state_up": port["admin_state_up"], "network_id": network_id, "status": port["status"] } path = PORTS_URI % (tenant_id, network_id) self.send_request("POST", path, body=jsonutils.dumps(lport), resource='port', tenant_id=tenant_id) LOG.debug(_("Port %(id)s created under tenant %(tenant_id)s"), {'id': port['id'], 'tenant_id': tenant_id})
def _create_lrouter_nat_rule(cluster, router_id, nat_rule_obj): LOG.debug(_("Creating NAT rule: %s"), nat_rule_obj) uri = _build_uri_path(LROUTERNAT_RESOURCE, parent_resource_id=router_id) return do_request(HTTP_POST, uri, jsonutils.dumps(nat_rule_obj), cluster=cluster)
def lease_update(cls): network_id = os.environ.get(cls.NEUTRON_NETWORK_ID_KEY) dhcp_relay_socket = os.environ.get(cls.NEUTRON_RELAY_SOCKET_PATH_KEY) action = sys.argv[1] if action not in ("add", "del", "old"): sys.exit() mac_address = sys.argv[2] ip_address = sys.argv[3] if action == "del": lease_remaining = 0 else: lease_remaining = int(os.environ.get("DNSMASQ_TIME_REMAINING", 0)) data = dict( network_id=network_id, mac_address=mac_address, ip_address=ip_address, lease_remaining=lease_remaining ) if os.path.exists(dhcp_relay_socket): sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(dhcp_relay_socket) sock.send(jsonutils.dumps(data)) sock.close()
def plug_router_port_attachment(cluster, router_id, port_id, attachment_uuid, nsx_attachment_type, attachment_vlan=None): """Attach a router port to the given attachment. Current attachment types: - PatchAttachment [-> logical switch port uuid] - L3GatewayAttachment [-> L3GatewayService uuid] For the latter attachment type a VLAN ID can be specified as well. """ uri = _build_uri_path(LROUTERPORT_RESOURCE, port_id, router_id, is_attachment=True) attach_obj = {} attach_obj["type"] = nsx_attachment_type if nsx_attachment_type == "PatchAttachment": attach_obj["peer_port_uuid"] = attachment_uuid elif nsx_attachment_type == "L3GatewayAttachment": attach_obj["l3_gateway_service_uuid"] = attachment_uuid if attachment_vlan: attach_obj['vlan_id'] = attachment_vlan else: raise nsx_exc.InvalidAttachmentType( attachment_type=nsx_attachment_type) return do_request( HTTP_PUT, uri, jsonutils.dumps(attach_obj), cluster=cluster)
def test_update_gw_device_without_certificate(self): # NOTE(salv-orlando): This unit test mocks backend calls rather than # leveraging the fake NSX API client display_name = 'fake-device' neutron_id = 'whatever' connector_type = 'stt' connector_ip = '1.1.1.1' with mock.patch.object(nsxlib, 'do_request') as request_mock: expected_req_body = self._create_expected_req_body( display_name, neutron_id, connector_type.upper(), connector_ip, None) l2gwlib.update_gateway_device(self.fake_cluster, 'whatever', 'fake_tenant', display_name, neutron_id, 'fake_tz_uuid', connector_type, connector_ip, client_certificate=None) request_mock.assert_called_once_with( "PUT", "/ws.v1/transport-node/whatever", jsonutils.dumps(expected_req_body, sort_keys=True), cluster=self.fake_cluster)
def __call__(self, target, creds): """ Check http: rules by calling to a remote server. This example implementation simply verifies that the response is exactly 'True'. """ url = ('http:' + self.match) % target data = { 'target': jsonutils.dumps(target), 'credentials': jsonutils.dumps(creds) } post_data = urllib.urlencode(data) f = urllib2.urlopen(url, post_data) return f.read() == "True"
def create_network(self, network): tenant_id = network['tenant_id'] router_external = network['router:external'] is True network_obj = { "name": network['name'], "tenant_id": tenant_id, "shared": network['shared'], "admin_state_up": network['admin_state_up'], "router:external": router_external } uri = NETWORKS_URI % tenant_id response = self.send_request("POST", uri, body=jsonutils.dumps(network_obj), resource='network', tenant_id=tenant_id) nvsd_net = response.json() LOG.debug(_("Network %(id)s created under tenant %(tenant_id)s"), { 'id': nvsd_net['id'], 'tenant_id': tenant_id }) return nvsd_net
def sendjson(self, method, urlpath, obj=None): """Send json to the OpenDaylight controller.""" headers = {'Content-Type': 'application/json'} data = jsonutils.dumps(obj, indent=2) if obj else None url = '/'.join([self.url, urlpath]) LOG.debug("Sending METHOD (%(method)s) URL (%(url)s) JSON (%(obj)s)" % {'method': method, 'url': url, 'obj': obj}) r = requests.request(method, url=url, headers=headers, data=data, auth=self.auth, timeout=self.timeout) try: r.raise_for_status() except Exception as ex: LOG.error("Error Sending METHOD (%(method)s) URL (%(url)s)" "JSON (%(obj)s) return: %(r)s ex: %(ex)s rtext: " "%(rtext)s" % {'method': method, 'url': url, 'obj': obj, 'r': r, 'ex': ex, 'rtext': r.text}) import pdb;pdb.set_trace() return r try: return json.loads(r.content) except Exception: LOG.debug("%s" % r) return
def set_binding(self, segment_id, vif_type, vif_details, status=None): # TODO(rkukura) Verify binding allowed, segment in network self._binding.segment = segment_id self._binding.vif_type = vif_type self._binding.vif_details = jsonutils.dumps(vif_details) self._new_port_status = status
def create_network(self, network): tenant_id = network['tenant_id'] router_external = network['router:external'] is True network_obj = { "name": network['name'], "tenant_id": tenant_id, "shared": network['shared'], "admin_state_up": network['admin_state_up'], "router:external": router_external } uri = NETWORKS_URI % tenant_id response = self.send_request("POST", uri, body=jsonutils.dumps(network_obj), resource='network', tenant_id=tenant_id) nvsd_net = response.json() LOG.debug(_("Network %(id)s created under tenant %(tenant_id)s"), {'id': nvsd_net['id'], 'tenant_id': tenant_id}) return nvsd_net
def sync_routers(self, context, **kwargs): """Sync routers according to filters to a specific agent. @param context: contain user information @param kwargs: host, router_ids @return: a list of routers with their interfaces and floating_ips """ router_ids = kwargs.get('router_ids') host = kwargs.get('host') context = neutron_context.get_admin_context() plugin = manager.NeutronManager.get_plugin() if utils.is_extension_supported( plugin, constants.L3_AGENT_SCHEDULER_EXT_ALIAS): if cfg.CONF.router_auto_schedule: plugin.auto_schedule_routers(context, host, router_ids) routers = plugin.list_active_sync_routers_on_active_l3_agent( context, host, router_ids) else: routers = plugin.get_sync_data(context, router_ids) if utils.is_extension_supported( plugin, constants.PORT_BINDING_EXT_ALIAS): self._ensure_host_set_on_ports(context, plugin, host, routers) LOG.debug(_("Routers returned to l3 agent:\n %s"), jsonutils.dumps(routers, indent=5)) return routers
def sync_routers(self, context, **kwargs): """Sync routers according to filters to a specific agent. @param context: contain user information @param kwargs: host, router_ids @return: a list of routers with their interfaces and floating_ips """ router_ids = kwargs.get("router_ids") host = kwargs.get("host") context = neutron_context.get_admin_context() l3plugin = manager.NeutronManager.get_service_plugins()[plugin_constants.L3_ROUTER_NAT] if not l3plugin: routers = {} LOG.error(_("No plugin for L3 routing registered! Will reply " "to l3 agent with empty router dictionary.")) elif utils.is_extension_supported(l3plugin, constants.L3_AGENT_SCHEDULER_EXT_ALIAS): if cfg.CONF.router_auto_schedule: l3plugin.auto_schedule_routers(context, host, router_ids) routers = l3plugin.list_active_sync_routers_on_active_l3_agent(context, host, router_ids) else: routers = l3plugin.get_sync_data(context, router_ids) plugin = manager.NeutronManager.get_plugin() if utils.is_extension_supported(plugin, constants.PORT_BINDING_EXT_ALIAS): self._ensure_host_set_on_ports(context, plugin, host, routers) LOG.debug(_("Routers returned to l3 agent:\n %s"), jsonutils.dumps(routers, indent=5)) return routers
def create_lqueue(cluster, queue_data): params = { 'name': 'display_name', 'qos_marking': 'qos_marking', 'min': 'min_bandwidth_rate', 'max': 'max_bandwidth_rate', 'dscp': 'dscp' } queue_obj = dict((nvp_name, queue_data.get(api_name)) for api_name, nvp_name in params.iteritems() if attr.is_attr_set(queue_data.get(api_name))) if 'display_name' in queue_obj: queue_obj['display_name'] = utils.check_and_truncate( queue_obj['display_name']) queue_obj['tags'] = utils.get_tags() try: return do_request(HTTP_POST, _build_uri_path(LQUEUE_RESOURCE), jsonutils.dumps(queue_obj), cluster=cluster)['uuid'] except NvpApiClient.NvpApiException: # FIXME(salv-orlando): This should not raise NeutronException with excutils.save_and_reraise_exception(): raise exception.NeutronException()
def update_port(self, tenant_id, port, port_update): network_id = port['network_id'] port_id = port['id'] lport = {} for k in ('admin_state_up', 'name', 'device_id', 'device_owner'): if k in port_update: lport[k] = port_update[k] fixed_ips = port_update.get('fixed_ips', None) if fixed_ips: lport["ip_address"] = fixed_ips[0].get("ip_address") lport["subnet_id"] = fixed_ips[0].get("subnet_id") uri = PORT_URI % (tenant_id, network_id, port_id) self.send_request("PUT", uri, body=jsonutils.dumps(lport), resource='port', tenant_id=tenant_id, resource_id=port_id) LOG.debug(_("Port %(id)s updated under tenant %(tenant_id)s"), { 'id': port_id, 'tenant_id': tenant_id })
def lease_update(cls): network_id = os.environ.get(cls.NEUTRON_NETWORK_ID_KEY) dhcp_relay_socket = os.environ.get(cls.NEUTRON_RELAY_SOCKET_PATH_KEY) action = sys.argv[1] if action not in ('add', 'del', 'old'): sys.exit() mac_address = sys.argv[2] ip_address = sys.argv[3] if action == 'del': lease_remaining = 0 else: lease_remaining = int(os.environ.get('DNSMASQ_TIME_REMAINING', 0)) data = dict(network_id=network_id, mac_address=mac_address, ip_address=ip_address, lease_remaining=lease_remaining) if os.path.exists(dhcp_relay_socket): sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(dhcp_relay_socket) sock.send(jsonutils.dumps(data)) sock.close()
def __init__(self, session, node_name, node_opts=None): """Init the Publisher class with the exchange_name, routing_key, and other options """ self.sender = None self.session = session addr_opts = { "create": "always", "node": { "type": "topic", "x-declare": { "durable": False, # auto-delete isn't implemented for exchanges in qpid, # but put in here anyway "auto-delete": True, }, }, } if node_opts: addr_opts["node"]["x-declare"].update(node_opts) self.address = "%s ; %s" % (node_name, jsonutils.dumps(addr_opts)) self.reconnect(session)
def format(self, record): message = {'message': record.getMessage(), 'asctime': self.formatTime(record, self.datefmt), 'name': record.name, 'msg': record.msg, 'args': record.args, 'levelname': record.levelname, 'levelno': record.levelno, 'pathname': record.pathname, 'filename': record.filename, 'module': record.module, 'lineno': record.lineno, 'funcname': record.funcName, 'created': record.created, 'msecs': record.msecs, 'relative_created': record.relativeCreated, 'thread': record.thread, 'thread_name': record.threadName, 'process_name': record.processName, 'process': record.process, 'traceback': None} if hasattr(record, 'extra'): message['extra'] = record.extra if record.exc_info: message['traceback'] = self.formatException(record.exc_info) return jsonutils.dumps(message)
def sync_routers(self, context, **kwargs): """Sync routers according to filters to a specific agent. @param context: contain user information @param kwargs: host, router_ids @return: a list of routers with their interfaces and floating_ips """ router_ids = kwargs.get('router_ids') host = kwargs.get('host') context = neutron_context.get_admin_context() if not self.l3plugin: routers = {} LOG.error( _('No plugin for L3 routing registered! Will reply ' 'to l3 agent with empty router dictionary.')) elif utils.is_extension_supported( self.l3plugin, constants.L3_AGENT_SCHEDULER_EXT_ALIAS): if cfg.CONF.router_auto_schedule: self.l3plugin.auto_schedule_routers(context, host, router_ids) routers = ( self.l3plugin.list_active_sync_routers_on_active_l3_agent( context, host, router_ids)) else: routers = self.l3plugin.get_sync_data(context, router_ids) if utils.is_extension_supported(self.plugin, constants.PORT_BINDING_EXT_ALIAS): self._ensure_host_set_on_ports(context, host, routers) LOG.debug(_("Routers returned to l3 agent:\n %s"), jsonutils.dumps(routers, indent=5)) return routers
def create_lqueue(cluster, queue_data): params = { 'name': 'display_name', 'qos_marking': 'qos_marking', 'min': 'min_bandwidth_rate', 'max': 'max_bandwidth_rate', 'dscp': 'dscp' } queue_obj = dict( (nsx_name, queue_data.get(api_name)) for api_name, nsx_name in params.iteritems() if attr.is_attr_set(queue_data.get(api_name)) ) if 'display_name' in queue_obj: queue_obj['display_name'] = utils.check_and_truncate( queue_obj['display_name']) queue_obj['tags'] = utils.get_tags() try: return do_request(HTTP_POST, _build_uri_path(LQUEUE_RESOURCE), jsonutils.dumps(queue_obj), cluster=cluster)['uuid'] except api_exc.NsxApiException: # FIXME(salv-orlando): This should not raise NeutronException with excutils.save_and_reraise_exception(): raise exception.NeutronException()
def mk_body(**kwargs): """Convenience function creates and dumps dictionary to string. :param kwargs: the key/value pirs to be dumped into a json string. :returns: a json string. """ return jsonutils.dumps(kwargs, ensure_ascii=False)
def create_l2_gw_service(cluster, tenant_id, display_name, devices): """Create a NSX Layer-2 Network Gateway Service. :param cluster: The target NSX cluster :param tenant_id: Identifier of the Openstack tenant for which the gateway service. :param display_name: Descriptive name of this gateway service :param devices: List of transport node uuids (and network interfaces on them) to use for the network gateway service :raise NsxApiException: if there is a problem while communicating with the NSX controller """ # NOTE(salvatore-orlando): This is a little confusing, but device_id in # NSX is actually the identifier a physical interface on the gateway # device, which in the Neutron API is referred as interface_name gateways = [{"transport_node_uuid": device['id'], "device_id": device['interface_name'], "type": "L2Gateway"} for device in devices] gwservice_obj = { "display_name": utils.check_and_truncate(display_name), "tags": utils.get_tags(os_tid=tenant_id), "gateways": gateways, "type": "L2GatewayServiceConfig" } return nsxlib.do_request( HTTP_POST, nsxlib._build_uri_path(GWSERVICE_RESOURCE), jsonutils.dumps(gwservice_obj), cluster=cluster)
def create_router_lport(cluster, lrouter_uuid, tenant_id, neutron_port_id, display_name, admin_status_enabled, ip_addresses, mac_address=None): """Creates a logical port on the assigned logical router.""" lport_obj = dict(admin_status_enabled=admin_status_enabled, display_name=display_name, tags=utils.get_tags(os_tid=tenant_id, q_port_id=neutron_port_id), ip_addresses=ip_addresses, type="LogicalRouterPortConfig") # Only add the mac_address to lport_obj if present. This is because # when creating the fake_ext_gw there is no mac_address present. if mac_address: lport_obj['mac_address'] = mac_address path = _build_uri_path(LROUTERPORT_RESOURCE, parent_resource_id=lrouter_uuid) result = do_request(HTTP_POST, path, jsonutils.dumps(lport_obj), cluster=cluster) LOG.debug( _("Created logical port %(lport_uuid)s on " "logical router %(lrouter_uuid)s"), { 'lport_uuid': result['uuid'], 'lrouter_uuid': lrouter_uuid }) return result
def plug_router_port_attachment(cluster, router_id, port_id, attachment_uuid, nsx_attachment_type, attachment_vlan=None): """Attach a router port to the given attachment. Current attachment types: - PatchAttachment [-> logical switch port uuid] - L3GatewayAttachment [-> L3GatewayService uuid] For the latter attachment type a VLAN ID can be specified as well. """ uri = _build_uri_path(LROUTERPORT_RESOURCE, port_id, router_id, is_attachment=True) attach_obj = {} attach_obj["type"] = nsx_attachment_type if nsx_attachment_type == "PatchAttachment": attach_obj["peer_port_uuid"] = attachment_uuid elif nsx_attachment_type == "L3GatewayAttachment": attach_obj["l3_gateway_service_uuid"] = attachment_uuid if attachment_vlan: attach_obj['vlan_id'] = attachment_vlan else: raise nvp_exc.NvpInvalidAttachmentType( attachment_type=nsx_attachment_type) return do_request(HTTP_PUT, uri, jsonutils.dumps(attach_obj), cluster=cluster)
def _do_request(self, method, resource, payload=None, more_headers=None, full_url=False): """Perform a REST request to a CSR resource. If this is the first time interacting with the CSR, a token will be obtained. If the request fails, due to an expired token, the token will be obtained and the request will be retried once more. """ if self.token is None: if not self.authenticate(): return if full_url: url = resource else: url = ('https://%(host)s/api/v1/%(resource)s' % {'host': self.host, 'resource': resource}) headers = {'Accept': 'application/json', 'X-auth-token': self.token} if more_headers: headers.update(more_headers) if payload: payload = jsonutils.dumps(payload) response = self._request(method, url, data=payload, headers=headers) if self.status == requests.codes.UNAUTHORIZED: if not self.authenticate(): return headers['X-auth-token'] = self.token response = self._request(method, url, data=payload, headers=headers) if self.status != requests.codes.REQUEST_TIMEOUT: return response LOG.error(_("%(method)s: Request timeout for CSR(%(host)s)"), {'method': method, 'host': self.host})
def update_lrouter_port_ips(cluster, lrouter_id, lport_id, ips_to_add, ips_to_remove): uri = _build_uri_path(LROUTERPORT_RESOURCE, lport_id, lrouter_id) try: port = do_request(HTTP_GET, uri, cluster=cluster) # TODO(salvatore-orlando): Enforce ips_to_add intersection with # ips_to_remove is empty ip_address_set = set(port['ip_addresses']) ip_address_set = ip_address_set - set(ips_to_remove) ip_address_set = ip_address_set | set(ips_to_add) # Set is not JSON serializable - convert to list port['ip_addresses'] = list(ip_address_set) do_request(HTTP_PUT, uri, jsonutils.dumps(port), cluster=cluster) except exception.NotFound as e: # FIXME(salv-orlando):avoid raising different exception data = {'lport_id': lport_id, 'lrouter_id': lrouter_id} msg = (_("Router Port %(lport_id)s not found on router " "%(lrouter_id)s") % data) LOG.exception(msg) raise nvp_exc.NvpPluginException(err_msg=msg) except NvpApiClient.NvpApiException as e: msg = _("An exception occurred while updating IP addresses on a " "router logical port:%s") % str(e) LOG.exception(msg) raise nvp_exc.NvpPluginException(err_msg=msg)
def _send_request(self, operation, url, payload, desc): """Send request to DCNM.""" res = None try: payload_json = None if payload and payload != '': payload_json = jsonutils.dumps(payload) self._login() desc_lookup = { 'POST': ' creation', 'PUT': ' update', 'DELETE': ' deletion', 'GET': ' get' } res = requests.request(operation, url, data=payload_json, headers=self._req_headers, timeout=self._TIMEOUT_RESPONSE) desc += desc_lookup.get(operation, operation.lower()) LOG.info(_("DCNM-send_request: %(desc)s %(url)s %(pld)s"), { 'desc': desc, 'url': url, 'pld': payload }) self._logout() except (requests.HTTPError, requests.Timeout, requests.ConnectionError) as e: LOG.exception(_('Error during request')) raise dexc.DFAClientRequestFailed(reason=e) return res
def update_router_lport(cluster, lrouter_uuid, lrouter_port_uuid, tenant_id, neutron_port_id, display_name, admin_status_enabled, ip_addresses): """Updates a logical port on the assigned logical router.""" lport_obj = dict( admin_status_enabled=admin_status_enabled, display_name=display_name, tags=utils.get_tags(os_tid=tenant_id, q_port_id=neutron_port_id), ip_addresses=ip_addresses, type="LogicalRouterPortConfig" ) # Do not pass null items to NSX for key in lport_obj.keys(): if lport_obj[key] is None: del lport_obj[key] path = _build_uri_path(LROUTERPORT_RESOURCE, lrouter_port_uuid, parent_resource_id=lrouter_uuid) result = do_request(HTTP_PUT, path, jsonutils.dumps(lport_obj), cluster=cluster) LOG.debug(_("Updated logical port %(lport_uuid)s on " "logical router %(lrouter_uuid)s"), {'lport_uuid': lrouter_port_uuid, 'lrouter_uuid': lrouter_uuid}) return result
def update_lrouter_port_ips(cluster, lrouter_id, lport_id, ips_to_add, ips_to_remove): uri = _build_uri_path(LROUTERPORT_RESOURCE, lport_id, lrouter_id) try: port = do_request(HTTP_GET, uri, cluster=cluster) # TODO(salvatore-orlando): Enforce ips_to_add intersection with # ips_to_remove is empty ip_address_set = set(port['ip_addresses']) ip_address_set = ip_address_set - set(ips_to_remove) ip_address_set = ip_address_set | set(ips_to_add) # Set is not JSON serializable - convert to list port['ip_addresses'] = list(ip_address_set) do_request(HTTP_PUT, uri, jsonutils.dumps(port), cluster=cluster) except exception.NotFound as e: # FIXME(salv-orlando):avoid raising different exception data = {'lport_id': lport_id, 'lrouter_id': lrouter_id} msg = (_("Router Port %(lport_id)s not found on router " "%(lrouter_id)s") % data) LOG.exception(msg) raise nsx_exc.NsxPluginException(err_msg=msg) except api_exc.NsxApiException as e: msg = _("An exception occurred while updating IP addresses on a " "router logical port:%s") % str(e) LOG.exception(msg) raise nsx_exc.NsxPluginException(err_msg=msg)
def create_l2_gw_service(cluster, tenant_id, display_name, devices): """Create a NSX Layer-2 Network Gateway Service. :param cluster: The target NSX cluster :param tenant_id: Identifier of the Openstack tenant for which the gateway service. :param display_name: Descriptive name of this gateway service :param devices: List of transport node uuids (and network interfaces on them) to use for the network gateway service :raise NsxApiException: if there is a problem while communicating with the NSX controller """ # NOTE(salvatore-orlando): This is a little confusing, but device_id in # NSX is actually the identifier a physical interface on the gateway # device, which in the Neutron API is referred as interface_name gateways = [{ "transport_node_uuid": device['id'], "device_id": device['interface_name'], "type": "L2Gateway" } for device in devices] gwservice_obj = { "display_name": utils.check_and_truncate(display_name), "tags": utils.get_tags(os_tid=tenant_id), "gateways": gateways, "type": "L2GatewayServiceConfig" } return nsxlib.do_request(HTTP_POST, nsxlib._build_uri_path(GWSERVICE_RESOURCE), jsonutils.dumps(gwservice_obj), cluster=cluster)
def _create_or_update_agent(self, context, agent): with context.session.begin(subtransactions=True): res_keys = ['agent_type', 'binary', 'host', 'topic'] res = dict((k, agent[k]) for k in res_keys) configurations_dict = agent.get('configurations', {}) res['configurations'] = jsonutils.dumps(configurations_dict) current_time = timeutils.utcnow() try: agent_db = self._get_agent_by_type_and_host( context, agent['agent_type'], agent['host']) res['heartbeat_timestamp'] = current_time if agent.get('start_flag'): res['started_at'] = current_time greenthread.sleep(0) agent_db.update(res) except ext_agent.AgentNotFoundByTypeHost: greenthread.sleep(0) res['created_at'] = current_time res['started_at'] = current_time res['heartbeat_timestamp'] = current_time res['admin_state_up'] = True agent_db = Agent(**res) greenthread.sleep(0) context.session.add(agent_db) greenthread.sleep(0)