qd_parsed_field_t *qd_message_message_annotations(qd_message_t *in_msg) { qd_message_pvt_t *msg = (qd_message_pvt_t*) in_msg; qd_message_content_t *content = msg->content; if (content->parsed_message_annotations) return content->parsed_message_annotations; qd_field_iterator_t *ma = qd_message_field_iterator(in_msg, QD_FIELD_MESSAGE_ANNOTATION); if (ma == 0) return 0; content->parsed_message_annotations = qd_parse(ma); if (content->parsed_message_annotations == 0 || !qd_parse_ok(content->parsed_message_annotations) || !qd_parse_is_map(content->parsed_message_annotations)) { qd_field_iterator_free(ma); qd_parse_free(content->parsed_message_annotations); content->parsed_message_annotations = 0; return 0; } qd_field_iterator_free(ma); return content->parsed_message_annotations; }
void qdra_link_update_CT(qdr_core_t *core, qd_field_iterator_t *name, qd_field_iterator_t *identity, qdr_query_t *query, qd_parsed_field_t *in_body) { // If the request was successful then the statusCode MUST contain 200 (OK) and the body of the message // MUST contain a map containing the actual attributes of the entity updated. These MAY differ from those // requested. // A map containing attributes that are not applicable for the entity being created, or invalid values for a // given attribute, MUST result in a failure response with a statusCode of 400 (Bad Request). if (qd_parse_is_map(in_body)) { // The absence of an attribute name implies that the entity should retain its existing value. // If the map contains a key-value pair where the value is null then the updated entity should have no value // for that attribute, removing any previous value. qd_parsed_field_t *admin_state = qd_parse_value_by_key(in_body, qdr_link_columns[QDR_LINK_ADMIN_STATE]); if (admin_state) { //admin state is the only field that can be updated via the update management request //qd_field_iterator_t *adm_state = qd_parse_raw(admin_state); if (identity) { qdr_link_t *link = qdr_link_find_by_identity(core, identity); // TODO - set the adm_state on the link qdra_link_update_set_status(core, query, link); } else if (name) { qdr_link_t *link = qdr_link_find_by_name(core, name); // TODO - set the adm_state on the link qdra_link_update_set_status(core, query, link); } else { qdra_link_set_bad_request(query); } } else qdra_link_set_bad_request(query); } else query->status = QD_AMQP_BAD_REQUEST; // // Enqueue the response. // qdr_agent_enqueue_response_CT(core, query); }
static void qd_core_agent_query_handler(qdr_core_t *core, qd_router_entity_type_t entity_type, qd_router_operation_type_t operation_type, qd_message_t *msg, int *count, int *offset) { // // Add the Body. // qd_composed_field_t *field = qd_compose(QD_PERFORMATIVE_BODY_AMQP_VALUE, 0); // Start a map in the body. Look for the end map in the callback function, qd_manage_response_handler. qd_compose_start_map(field); //add a "attributeNames" key qd_compose_insert_string(field, ATTRIBUTE_NAMES); // Call local function that creates and returns a local qd_management_context_t object containing the values passed in. qd_management_context_t *ctx = qd_management_context(qd_message(), msg, field, 0, core, operation_type, (*count)); // Grab the attribute names from the incoming message body. The attribute names will be used later on in the response. qd_parsed_field_t *attribute_names_parsed_field = 0; qd_field_iterator_t *body_iter = qd_message_field_iterator(msg, QD_FIELD_BODY); qd_parsed_field_t *body = qd_parse(body_iter); if (body != 0 && qd_parse_is_map(body)) { attribute_names_parsed_field = qd_parse_value_by_key(body, ATTRIBUTE_NAMES); } // Set the callback function. qdr_manage_handler(core, qd_manage_response_handler); ctx->query = qdr_manage_query(core, ctx, entity_type, attribute_names_parsed_field, field); //Add the attribute names qdr_query_add_attribute_names(ctx->query); //this adds a list of attribute names like ["attribute1", "attribute2", "attribute3", "attribute4",] qd_compose_insert_string(field, results); //add a "results" key qd_compose_start_list(field); //start the list for results qdr_query_get_first(ctx->query, (*offset)); qd_field_iterator_free(body_iter); qd_parse_free(body); }
void qdra_config_auto_link_create_CT(qdr_core_t *core, qd_field_iterator_t *name, qdr_query_t *query, qd_parsed_field_t *in_body) { while (true) { // // Ensure there isn't a duplicate name and that the body is a map // qdr_auto_link_t *al = DEQ_HEAD(core->auto_links); while (al) { if (name && al->name && qd_field_iterator_equal(name, (const unsigned char*) al->name)) break; al = DEQ_NEXT(al); } if (!!al) { query->status = QD_AMQP_BAD_REQUEST; query->status.description = "Name conflicts with an existing entity"; qd_log(core->agent_log, QD_LOG_ERROR, "Error performing CREATE of %s: %s", CONFIG_AUTOLINK_TYPE, query->status.description); break; } if (!qd_parse_is_map(in_body)) { query->status = QD_AMQP_BAD_REQUEST; query->status.description = "Body of request must be a map"; qd_log(core->agent_log, QD_LOG_ERROR, "Error performing CREATE of %s: %s", CONFIG_AUTOLINK_TYPE, query->status.description); break; } // // Extract the fields from the request // qd_parsed_field_t *addr_field = qd_parse_value_by_key(in_body, qdr_config_auto_link_columns[QDR_CONFIG_AUTO_LINK_ADDR]); qd_parsed_field_t *dir_field = qd_parse_value_by_key(in_body, qdr_config_auto_link_columns[QDR_CONFIG_AUTO_LINK_DIR]); qd_parsed_field_t *phase_field = qd_parse_value_by_key(in_body, qdr_config_auto_link_columns[QDR_CONFIG_AUTO_LINK_PHASE]); qd_parsed_field_t *connection_field = qd_parse_value_by_key(in_body, qdr_config_auto_link_columns[QDR_CONFIG_AUTO_LINK_CONNECTION]); qd_parsed_field_t *container_field = qd_parse_value_by_key(in_body, qdr_config_auto_link_columns[QDR_CONFIG_AUTO_LINK_CONTAINER_ID]); // // Addr and dir fields are mandatory. Fail if they're not both here. // if (!addr_field || !dir_field) { query->status = QD_AMQP_BAD_REQUEST; query->status.description = "addr and dir fields are mandatory"; qd_log(core->agent_log, QD_LOG_ERROR, "Error performing CREATE of %s: %s", CONFIG_AUTOLINK_TYPE, query->status.description); break; } qd_direction_t dir; const char *error = qdra_auto_link_direction_CT(dir_field, &dir); if (error) { query->status = QD_AMQP_BAD_REQUEST; query->status.description = error; qd_log(core->agent_log, QD_LOG_ERROR, "Error performing CREATE of %s: %s", CONFIG_AUTOLINK_TYPE, query->status.description); break; } // // Use the specified phase if present. Otherwise default based on the direction: // Phase 0 for outgoing links and phase 1 for incoming links. // int phase = phase_field ? qd_parse_as_int(phase_field) : (dir == QD_OUTGOING ? 0 : 1); // // Validate the phase // if (phase < 0 || phase > 9) { query->status = QD_AMQP_BAD_REQUEST; query->status.description = "autoLink phase must be between 0 and 9"; qd_log(core->agent_log, QD_LOG_ERROR, "Error performing CREATE of %s: %s", CONFIG_AUTOLINK_TYPE, query->status.description); break; } // // The request is good. Create the entity. // bool is_container = !!container_field; qd_parsed_field_t *in_use_conn = is_container ? container_field : connection_field; al = qdr_route_add_auto_link_CT(core, name, addr_field, dir, phase, in_use_conn, is_container); // // Compose the result map for the response. // if (query->body) { qd_compose_start_map(query->body); for (int col = 0; col < QDR_CONFIG_AUTO_LINK_COLUMN_COUNT; col++) qdr_config_auto_link_insert_column_CT(al, col, query->body, true); qd_compose_end_map(query->body); } query->status = QD_AMQP_CREATED; break; } // // Enqueue the response if there is a body. If there is no body, this is a management // operation created internally by the configuration file parser. // if (query->body) { // // If there was an error in processing the create, insert a NULL value into the body. // if (query->status.status / 100 > 2) qd_compose_insert_null(query->body); qdr_agent_enqueue_response_CT(core, query); } else { if (query->status.status / 100 > 2) qd_log(core->log, QD_LOG_ERROR, "Error configuring linkRoute: %s", query->status.description); qdr_query_free(query); } }
void qdra_conn_link_route_create_CT(qdr_core_t *core, qd_iterator_t *name, qdr_query_t *query, qd_parsed_field_t *in_body) { char *pattern = NULL; query->status = QD_AMQP_BAD_REQUEST; // fail if creating via a configuration file if (query->in_conn == 0) { query->status.description = "Can only create via management CREATE"; goto exit; } // find the associated connection qdr_connection_t *conn = _find_conn_CT(core, query->in_conn); if (!conn) { query->status.description = "Parent connection no longer exists"; goto exit; } // fail if forbidden by policy if (!conn->policy_allow_dynamic_link_routes) { query->status = QD_AMQP_FORBIDDEN; goto exit; } if (!qd_parse_is_map(in_body)) { query->status.description = "Body of request must be a map"; goto exit; } // // Extract the fields from the request // qd_parsed_field_t *pattern_field = qd_parse_value_by_key(in_body, qdr_conn_link_route_columns[QDR_CONN_LINK_ROUTE_PATTERN]); qd_parsed_field_t *dir_field = qd_parse_value_by_key(in_body, qdr_conn_link_route_columns[QDR_CONN_LINK_ROUTE_DIRECTION]); if (!pattern_field) { query->status.description = "Pattern field is required"; goto exit; } const char *error = NULL; pattern = qdra_config_address_validate_pattern_CT(pattern_field, false, &error); if (!pattern) { query->status.description = error; goto exit; } qd_direction_t dir; error = qdra_link_route_direction_CT(dir_field, &dir); if (error) { query->status.description = error; goto exit; } qdr_link_route_t *lr = qdr_route_add_conn_route_CT(core, conn, name, pattern, dir); if (!lr) { query->status.description = "creation failed"; goto exit; } query->status = QD_AMQP_CREATED; _write_as_map_CT(query, lr); exit: free(pattern); if (query->status.status != QD_AMQP_CREATED.status) { qd_log(core->agent_log, QD_LOG_ERROR, "Error performing CREATE of %s: %s", CONN_LINK_ROUTE_TYPE, query->status.description); qd_compose_insert_null(query->body); // no body map } qdr_agent_enqueue_response_CT(core, query); }
static char *test_map(void *context) { static char error[1000]; const char *data = "\xd1\x00\x00\x00\x2d\x00\x00\x00\x06" // map32, 6 items "\xa3\x05\x66irst\xa1\x0evalue_of_first" // (23) "first":"value_of_first" "\xa3\x06second\x52\x20" // (10) "second":32 "\xa3\x05third\x41"; // (8) "third":true int data_len = 50; qd_iterator_t *data_iter = qd_iterator_binary(data, data_len, ITER_VIEW_ALL); qd_parsed_field_t *field = qd_parse(data_iter); if (!qd_parse_ok(field)) { snprintf(error, 1000, "Parse failed: %s", qd_parse_error(field)); qd_iterator_free(data_iter); qd_parse_free(field); return error; } if (!qd_parse_is_map(field)) { qd_iterator_free(data_iter); qd_parse_free(field); return "Expected field to be a map"; } uint32_t count = qd_parse_sub_count(field); if (count != 3) { snprintf(error, 1000, "Expected sub-count==3, got %"PRIu32, count); qd_iterator_free(data_iter); qd_parse_free(field); return error; } qd_parsed_field_t *key_field = qd_parse_sub_key(field, 0); qd_iterator_t *key_iter = qd_parse_raw(key_field); qd_iterator_t *typed_iter = qd_parse_typed(key_field); if (!qd_iterator_equal(key_iter, (unsigned char*) "first")) { unsigned char *result = qd_iterator_copy(key_iter); snprintf(error, 1000, "First key: expected 'first', got '%s'", result); free (result); return error; } if (!qd_iterator_equal(typed_iter, (unsigned char*) "\xa3\x05\x66irst")) return "Incorrect typed iterator on first-key"; qd_parsed_field_t *val_field = qd_parse_sub_value(field, 0); qd_iterator_t *val_iter = qd_parse_raw(val_field); typed_iter = qd_parse_typed(val_field); if (!qd_iterator_equal(val_iter, (unsigned char*) "value_of_first")) { unsigned char *result = qd_iterator_copy(val_iter); snprintf(error, 1000, "First value: expected 'value_of_first', got '%s'", result); free (result); return error; } if (!qd_iterator_equal(typed_iter, (unsigned char*) "\xa1\x0evalue_of_first")) return "Incorrect typed iterator on first-key"; key_field = qd_parse_sub_key(field, 1); key_iter = qd_parse_raw(key_field); if (!qd_iterator_equal(key_iter, (unsigned char*) "second")) { unsigned char *result = qd_iterator_copy(key_iter); snprintf(error, 1000, "Second key: expected 'second', got '%s'", result); free (result); return error; } val_field = qd_parse_sub_value(field, 1); if (qd_parse_as_uint(val_field) != 32) { snprintf(error, 1000, "Second value: expected 32, got %"PRIu32, qd_parse_as_uint(val_field)); return error; } key_field = qd_parse_sub_key(field, 2); key_iter = qd_parse_raw(key_field); if (!qd_iterator_equal(key_iter, (unsigned char*) "third")) { unsigned char *result = qd_iterator_copy(key_iter); snprintf(error, 1000, "Third key: expected 'third', got '%s'", result); free (result); return error; } val_field = qd_parse_sub_value(field, 2); if (!qd_parse_as_bool(val_field)) { snprintf(error, 1000, "Third value: expected true"); return error; } qd_iterator_free(data_iter); qd_parse_free(field); return 0; }
void qdra_config_link_route_create_CT(qdr_core_t *core, qd_field_iterator_t *name, qdr_query_t *query, qd_parsed_field_t *in_body) { while (true) { // // Ensure there isn't a duplicate name and that the body is a map // qdr_link_route_t *lr = DEQ_HEAD(core->link_routes); while (lr) { if (name && lr->name && qd_field_iterator_equal(name, (const unsigned char*) lr->name)) break; lr = DEQ_NEXT(lr); } if (!!lr) { query->status = QD_AMQP_BAD_REQUEST; query->status.description = "Name conflicts with an existing entity"; break; } if (!qd_parse_is_map(in_body)) { query->status = QD_AMQP_BAD_REQUEST; break; } // // Extract the fields from the request // qd_parsed_field_t *prefix_field = qd_parse_value_by_key(in_body, qdr_config_link_route_columns[QDR_CONFIG_LINK_ROUTE_PREFIX]); qd_parsed_field_t *distrib_field = qd_parse_value_by_key(in_body, qdr_config_link_route_columns[QDR_CONFIG_LINK_ROUTE_DISTRIBUTION]); qd_parsed_field_t *connection_field = qd_parse_value_by_key(in_body, qdr_config_link_route_columns[QDR_CONFIG_LINK_ROUTE_CONNECTION]); qd_parsed_field_t *container_field = qd_parse_value_by_key(in_body, qdr_config_link_route_columns[QDR_CONFIG_LINK_ROUTE_CONTAINER_ID]); qd_parsed_field_t *dir_field = qd_parse_value_by_key(in_body, qdr_config_link_route_columns[QDR_CONFIG_LINK_ROUTE_DIR]); // // Prefix and dir fields are mandatory. Fail if they're not both here. // if (!prefix_field || !dir_field) { query->status = QD_AMQP_BAD_REQUEST; break; } qd_direction_t dir; const char *error = qdra_link_route_direction_CT(dir_field, &dir); if (error) { query->status = QD_AMQP_BAD_REQUEST; query->status.description = error; break; } qd_address_treatment_t trt; error = qdra_link_route_treatment_CT(distrib_field, &trt); if (error) { query->status = QD_AMQP_BAD_REQUEST; query->status.description = error; break; } // // The request is good. Create the entity. // bool is_container = !!container_field; qd_parsed_field_t *in_use_conn = is_container ? container_field : connection_field; lr = qdr_route_add_link_route_CT(core, name, prefix_field, in_use_conn, is_container, trt, dir); // // Compose the result map for the response. // if (query->body) { qd_compose_start_map(query->body); for (int col = 0; col < QDR_CONFIG_LINK_ROUTE_COLUMN_COUNT; col++) qdr_config_link_route_insert_column_CT(lr, col, query->body, true); qd_compose_end_map(query->body); } query->status = QD_AMQP_CREATED; break; } // // Enqueue the response if there is a body. If there is no body, this is a management // operation created internally by the configuration file parser. // if (query->body) { // // If there was an error in processing the create, insert a NULL value into the body. // if (query->status.status / 100 > 2) qd_compose_insert_null(query->body); qdr_agent_enqueue_response_CT(core, query); } else { if (query->status.status / 100 > 2) qd_log(core->log, QD_LOG_ERROR, "Error configuring linkRoute: %s", query->status.description); qdr_query_free(query); } }
/** * Checks the content of the message to see if this can be handled by the C-management agent. If this agent cannot handle it, it will be * forwarded to the Python agent. */ static bool qd_can_handle_request(qd_parsed_field_t *properties_fld, qd_router_entity_type_t *entity_type, qd_router_operation_type_t *operation_type, qd_field_iterator_t **identity_iter, qd_field_iterator_t **name_iter, int *count, int *offset) { // The must be a property field and that property field should be a AMQP map. This is true for QUERY but I need // to check if it true for CREATE, UPDATE and DELETE if (properties_fld == 0 || !qd_parse_is_map(properties_fld)) return false; // // Only certain entity types can be handled by this agent. // 'entityType': 'org.apache.qpid.dispatch.router.address // 'entityType': 'org.apache.qpid.dispatch.router.link' // TODO - Add more entity types here. The above is not a complete list. qd_parsed_field_t *parsed_field = qd_parse_value_by_key(properties_fld, IDENTITY); if (parsed_field!=0) { *identity_iter = qd_parse_raw(parsed_field); } parsed_field = qd_parse_value_by_key(properties_fld, NAME); if (parsed_field!=0) { *name_iter = qd_parse_raw(parsed_field); } parsed_field = qd_parse_value_by_key(properties_fld, ENTITY); if (parsed_field == 0) { // Sometimes there is no 'entityType' but 'type' might be available. parsed_field = qd_parse_value_by_key(properties_fld, TYPE); if (parsed_field == 0) return false; } if (qd_field_iterator_equal(qd_parse_raw(parsed_field), address_entity_type)) *entity_type = QD_ROUTER_ADDRESS; else if (qd_field_iterator_equal(qd_parse_raw(parsed_field), link_entity_type)) *entity_type = QD_ROUTER_LINK; else if (qd_field_iterator_equal(qd_parse_raw(parsed_field), config_address_entity_type)) *entity_type = QD_ROUTER_CONFIG_ADDRESS; else if (qd_field_iterator_equal(qd_parse_raw(parsed_field), link_route_entity_type)) *entity_type = QD_ROUTER_CONFIG_LINK_ROUTE; else if (qd_field_iterator_equal(qd_parse_raw(parsed_field), auto_link_entity_type)) *entity_type = QD_ROUTER_CONFIG_AUTO_LINK; else if (qd_field_iterator_equal(qd_parse_raw(parsed_field), console_entity_type)) *entity_type = QD_ROUTER_FORBIDDEN; else return false; parsed_field = qd_parse_value_by_key(properties_fld, OPERATION); if (parsed_field == 0) return false; if (qd_field_iterator_equal(qd_parse_raw(parsed_field), MANAGEMENT_QUERY)) (*operation_type) = QD_ROUTER_OPERATION_QUERY; else if (qd_field_iterator_equal(qd_parse_raw(parsed_field), MANAGEMENT_CREATE)) (*operation_type) = QD_ROUTER_OPERATION_CREATE; else if (qd_field_iterator_equal(qd_parse_raw(parsed_field), MANAGEMENT_READ)) (*operation_type) = QD_ROUTER_OPERATION_READ; else if (qd_field_iterator_equal(qd_parse_raw(parsed_field), MANAGEMENT_UPDATE)) (*operation_type) = QD_ROUTER_OPERATION_UPDATE; else if (qd_field_iterator_equal(qd_parse_raw(parsed_field), MANAGEMENT_DELETE)) (*operation_type) = QD_ROUTER_OPERATION_DELETE; else // This is an unknown operation type. cannot be handled, return false. return false; // Obtain the count and offset. parsed_field = qd_parse_value_by_key(properties_fld, COUNT); if (parsed_field) (*count) = qd_parse_as_int(parsed_field); else (*count) = -1; parsed_field = qd_parse_value_by_key(properties_fld, OFFSET); if (parsed_field) (*offset) = qd_parse_as_int(parsed_field); else (*offset) = 0; return true; }