void
test_ofp_role_reply_create_with_null_agument(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct channel *channel =
    channel_alloc_ip4addr("127.0.0.1", "1000", 0x01);
  struct pbuf *pbuf;
  struct ofp_header xid_header;
  struct ofp_role_request role_request;
  role_request.role = OFPCR_ROLE_SLAVE;
  role_request.generation_id = 0x01;

  /* TODO add error as a 4th argument */
  /* struct ofp_error error; */
  ret = ofp_role_reply_create(NULL, &pbuf, &xid_header, &role_request);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_INVALID_ARGS, ret,
                            "NULL should be treated as invalid arguments");
  ret = ofp_role_reply_create(channel, NULL, &xid_header, &role_request);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_INVALID_ARGS, ret,
                            "NULL should be treated as invalid arguments");
  ret = ofp_role_reply_create(channel, &pbuf, NULL, &role_request);
  TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_INVALID_ARGS, ret,
                            "NULL should be treated as invalid arguments");

  channel_free(channel);
}
static lagopus_result_t
s_ofp_role_reply_create_wrap(struct channel *channel,
                             struct pbuf **pbuf,
                             struct ofp_header *xid_header) {
  struct ofp_role_request role_request;
  role_request.role = OFPCR_ROLE_SLAVE;
  role_request.generation_id = 0x01;

  return ofp_role_reply_create(channel, pbuf, xid_header, &role_request);
}
lagopus_result_t
ofp_role_request_handle(struct channel *channel,
                        struct pbuf *pbuf,
                        struct ofp_header *xid_header,
                        struct ofp_error *error) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  struct ofp_role_request role_request;
  struct ofp_role_request modified_role_request;
  struct pbuf *send_pbuf = NULL;
  uint64_t dpid;

  if (channel != NULL && pbuf != NULL &&
      xid_header != NULL && error != NULL) {
    /* Parse packet. */
    ret = ofp_role_request_decode(pbuf, &role_request);

    if (ret == LAGOPUS_RESULT_OK) {
      /* dump trace. */
      role_request_trace(&role_request);

      ret = role_check(role_request.role, error);

      if (ret == LAGOPUS_RESULT_OK) {
        dpid = channel_dpid_get(channel);
        if (ofp_role_generation_id_check(dpid, role_request.role,
                                         role_request.generation_id) == true) {
          /* copy role request. */
          modified_role_request = role_request;
          ret = ofp_role_channel_update(channel,
                                        &modified_role_request,
                                        dpid);
          if (ret == LAGOPUS_RESULT_OK) {
            /* Reply send.*/
            ret = ofp_role_reply_create(channel, &send_pbuf,
                                        xid_header, &modified_role_request);
            if (ret == LAGOPUS_RESULT_OK) {
              channel_send_packet(channel, send_pbuf);
            } else {
              lagopus_msg_warning("FAILED (%s).\n",
                                  lagopus_error_get_string(ret));
            }
          } else {
            lagopus_msg_warning("FAILED (%s).\n",
                                lagopus_error_get_string(ret));
          }
        } else {
          lagopus_msg_warning("bad generation_id.\n");
          ofp_error_set(error, OFPET_ROLE_REQUEST_FAILED, OFPRRFC_STALE);
          ret = LAGOPUS_RESULT_OFP_ERROR;
        }

        if (ret != LAGOPUS_RESULT_OK && send_pbuf != NULL) {
          channel_pbuf_list_unget(channel, send_pbuf);
        }
      }
    } else {
      lagopus_msg_warning("FAILED (%s).\n", lagopus_error_get_string(ret));
      ofp_error_set(error, OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
      ret = LAGOPUS_RESULT_OFP_ERROR;
    }
  } else {
    ret = LAGOPUS_RESULT_INVALID_ARGS;
  }

  return ret;
}