ValidationStatus validate_dom(XMLDocument* dom, rw_uagent::Instance* inst)
{
  RW_ASSERT (dom);
  rwlog_ctx_t* log = nullptr;

  if (!inst) {
    log = rwlog_init("RW.XMLDomValidation");
  } else {
    log = inst->rwlog();
  }

  XMLNode* root_xml_node = dom->get_root_node();
  ValidationStatus result = walk_xml_dom_and_validate(dom, root_xml_node, log);

  if (!inst) {
    rwlog_close(log, false);
  }

  return result;
}
Beispiel #2
0
struct rwmain_gi * rwmain_alloc(
    rwvx_instance_ptr_t rwvx,
    const char * component_name,
    uint32_t instance_id,
    const char * component_type,
    const char * parent_id,
    const char * vm_ip_address,
    uint32_t vm_instance_id)
{
  rw_status_t status;
  int r;
  rwtasklet_info_ptr_t info = NULL;
  rwdts_api_t * dts = NULL;
  struct rwmain_gi * rwmain = NULL;
  char * instance_name = NULL;


  rwmain = (struct rwmain_gi *)malloc(sizeof(struct rwmain_gi));
  if (!rwmain) {
    RW_CRASH();
    goto err;
  }
  bzero(rwmain, sizeof(struct rwmain_gi));

  /* If the component name wasn't specified on the command line, pull it
   * from the manifest init-phase.
   */
  if (!component_name) {
    char cn[1024];

    status = rwvcs_variable_evaluate_str(
        rwvx->rwvcs,
        "$rw_component_name",
        cn,
        sizeof(cn));
    if (status != RW_STATUS_SUCCESS) {
      RW_CRASH();
      goto err;
    }

    rwmain->component_name = strdup(cn);
  } else {
    rwmain->component_name = strdup(component_name);
  }

  if (!rwmain->component_name) {
    RW_CRASH();
    goto err;
  }


  /* If the instance id wasn't specified on the command line pull it from
   * the manifest init-phase if it is there, otherwise autoassign one.
   */
  if (instance_id == 0) {
    int id;

    status = rwvcs_variable_evaluate_int(
        rwvx->rwvcs,
        "$instance_id",
        &id);
    if ((status == RW_STATUS_SUCCESS) && id) {
      rwmain->instance_id = (uint32_t)id;
    } else {
      status = rwvcs_rwzk_next_instance_id(rwvx->rwvcs, &rwmain->instance_id, NULL);
      if (status != RW_STATUS_SUCCESS) {
        RW_CRASH();
        goto err;
      }
    }
  } else {
    rwmain->instance_id = instance_id;
  }

  if (component_type) {
    rwmain->component_type = component_type_str_to_enum(component_type);
  } else {
    char ctype[64];
    status = rwvcs_variable_evaluate_str(
        rwvx->rwvcs,
        "$component_type",
        ctype,
        sizeof(ctype));
    if (status != RW_STATUS_SUCCESS) {
      RW_CRASH();
      goto err;
    }
    rwmain->component_type = component_type_str_to_enum(ctype);
  }

  if (vm_instance_id > 0)
    rwmain->vm_instance_id = vm_instance_id;
  else if (rwmain->component_type == RWVCS_TYPES_COMPONENT_TYPE_RWVM)
    rwmain->vm_instance_id = rwmain->instance_id;
  else {
    int vm_instance_id;
    status = rwvcs_variable_evaluate_int(
        rwvx->rwvcs,
        "$vm_instance_id",
        &vm_instance_id);
    if (status == RW_STATUS_SUCCESS) {
      rwmain->vm_instance_id = (uint32_t)vm_instance_id;
    }
  }
  RW_ASSERT(rwmain->vm_instance_id);


  // 10 hz with tolerance 600
  // TODO: Must take from YANG. These are currently defined as the defaults in rw-base.yang
  rwmain->rwproc_heartbeat = rwproc_heartbeat_alloc(10, 600);
  if (!rwmain->rwproc_heartbeat) {
    RW_CRASH();
    goto err;
  }

  bzero(&rwmain->sys, sizeof(rwmain->sys));

  if (parent_id) {
    rwmain->parent_id = strdup(parent_id);
    if (!rwmain->parent_id) {
      RW_CRASH();
      goto err;
    }
  }
  else {
    char ctype[64];
    status = rwvcs_variable_evaluate_str(
        rwvx->rwvcs,
        "$parent_id",
        ctype,
        sizeof(ctype));
    if (status == RW_STATUS_SUCCESS) {
      rwmain->parent_id = strdup(ctype);
    }
  }


  if (rwvx->rwvcs->pb_rwmanifest->init_phase->settings->rwvcs->collapse_each_rwvm) {
    r = asprintf(&rwmain->vm_ip_address, "127.%u.%u.1", rwmain->instance_id / 256, rwmain->instance_id % 256);
    if (r == -1) {
      RW_CRASH();
      goto err;
    }
  } else if (vm_ip_address) {
    rwmain->vm_ip_address = strdup(vm_ip_address);
    if (!rwmain->vm_ip_address) {
      RW_CRASH();
      goto err;
    }
    char *variable[0];
    r = asprintf(&variable[0], "vm_ip_address = '%s'", vm_ip_address);
    if (r == -1) {
      RW_CRASH();
      goto err;
    }
    status = rwvcs_variable_list_evaluate(
        rwvx->rwvcs,
        1,
        variable);
    if (status != RW_STATUS_SUCCESS) {
      RW_CRASH();
      goto err;
    }
    free(variable[0]);
  } else {
    char buf[32];

    status = rwvcs_variable_evaluate_str(
        rwvx->rwvcs,
        "$vm_ip_address",
        buf,
        sizeof(buf));
    if (status != RW_STATUS_SUCCESS) {
      RW_CRASH();
      goto err;
    }

    rwmain->vm_ip_address = strdup(buf);
    if (!rwmain->vm_ip_address) {
      RW_CRASH();
      goto err;
    }
  }


  rwvx->rwvcs->identity.vm_ip_address = strdup(rwmain->vm_ip_address);
  if (!rwvx->rwvcs->identity.vm_ip_address) {
    RW_CRASH();
    goto err;
  }
  rwvx->rwvcs->identity.rwvm_instance_id = rwmain->vm_instance_id;

  instance_name = to_instance_name(rwmain->component_name, rwmain->instance_id);
  RW_ASSERT(instance_name!=NULL);
  if (rwmain->component_type == RWVCS_TYPES_COMPONENT_TYPE_RWVM) {
    rwvx->rwvcs->identity.rwvm_name = instance_name;
  }
  else if (rwmain->component_type == RWVCS_TYPES_COMPONENT_TYPE_RWPROC) {
    RW_ASSERT(rwmain->parent_id);
    rwvx->rwvcs->identity.rwvm_name = strdup(rwmain->parent_id);
  }

  char rift_var_vm[255];
  if (rwvx->rwvcs->identity.rwvm_name) {
    snprintf(rift_var_vm, 255, "%s%c%s", rwvx->rwvcs->pb_rwmanifest->bootstrap_phase->test_name,
             '-', rwvx->rwvcs->identity.rwvm_name);
  } else {
    snprintf(rift_var_vm, 255, "%s", rwvx->rwvcs->pb_rwmanifest->bootstrap_phase->test_name);
  }

  setenv("RIFT_VAR_VM", rift_var_vm, true);
  info = get_rwmain_tasklet_info(
      rwvx,
      rwmain->component_name,
      rwmain->instance_id,
      rwmain->vm_instance_id);
  if (!info) {
    RW_CRASH();
    goto err;
  }

  if (rwvx->rwsched) {
    if (!rwvx->rwsched->rwlog_instance) {
      rwvx->rwsched->rwlog_instance = rwlog_init("RW.Sched");
    }
  }
  if (!rwvx->rwlog) {
    rwvx->rwlog = rwlog_init("Logging");
  }

  dts = rwdts_api_new(
      info,
      (rw_yang_pb_schema_t *)RWPB_G_SCHEMA_YPBCSD(RwVcs),
      rwmain_dts_handle_state_change,
      NULL,
      NULL);

  if (!dts) {
    RW_CRASH();
    goto err;
  }

  RW_SKLIST_PARAMS_DECL(
      procs_sklist_params,
      struct rwmain_proc,
      instance_name,
      rw_sklist_comp_charptr,
      _sklist);
  RW_SKLIST_INIT(&(rwmain->procs), &procs_sklist_params);

  RW_SKLIST_PARAMS_DECL(
      tasklets_sklist_params,
      struct rwmain_tasklet,
      instance_name,
      rw_sklist_comp_charptr,
      _sklist);
  RW_SKLIST_INIT(&(rwmain->tasklets), &tasklets_sklist_params);

  RW_SKLIST_PARAMS_DECL(
      multivms_sklist_params,
      struct rwmain_multivm,
      key,
      rw_sklist_comp_charbuf,
      _sklist);
  RW_SKLIST_INIT(&(rwmain->multivms), &multivms_sklist_params);


  rwmain->dts = dts;
  rwmain->tasklet_info = info;
  rwmain->rwvx = rwvx;
  r = asprintf(&VCS_GET(rwmain)->vcs_instance_xpath,
               VCS_INSTANCE_XPATH_FMT,
               instance_name);
  if (r == -1) {
    RW_CRASH();
    goto err;
  }
  VCS_GET(rwmain)->instance_name = instance_name;

  goto done;

err:
  if (info) {
    rwsched_tasklet_free(info->rwsched_tasklet_info);
    free(info->identity.rwtasklet_name);
    rwmsg_endpoint_halt(info->rwmsg_endpoint);
    free(info);
  }

  if (dts)
    rwdts_api_deinit(dts);

  if (rwmain->component_name)
    free(rwmain->component_name);

  if (rwmain->parent_id)
    free(rwmain->parent_id);

  if (rwmain)
    free(rwmain);

done:

  return rwmain;
}
Beispiel #3
0
static rwtasklet_info_ptr_t get_rwmain_tasklet_info(
    rwvx_instance_ptr_t rwvx,
    const char * component_name,
    int instance_id,
    uint32_t vm_instance_id)
{
  rwtasklet_info_ptr_t info;
  char * instance_name = NULL;
  int broker_instance_id;

  info = (rwtasklet_info_ptr_t)RW_MALLOC0(sizeof(struct rwtasklet_info_s));
  if (!info) {
    RW_CRASH();
    goto err;
  }

  instance_name = to_instance_name(component_name, instance_id);
  if (!instance_name) {
    RW_CRASH();
    goto err;
  }

  info->rwsched_instance = rwvx->rwsched;
  info->rwsched_tasklet_info = rwsched_tasklet_new(rwvx->rwsched);
  info->rwtrace_instance = rwvx->rwtrace;
  info->rwvx = rwvx;
  info->rwvcs = rwvx->rwvcs;

  info->identity.rwtasklet_instance_id = instance_id;
  info->identity.rwtasklet_name = strdup(component_name);
  char *rift_var_root = rwtasklet_info_get_rift_var_root(info);
  RW_ASSERT(rift_var_root);

  rw_status_t status = rw_setenv("RIFT_VAR_ROOT", rift_var_root);
  RW_ASSERT(status == RW_STATUS_SUCCESS);
  setenv("RIFT_VAR_ROOT", rift_var_root, true);

  info->rwlog_instance = rwlog_init(instance_name);

  broker_instance_id = 0;
  if (rwvx->rwvcs->pb_rwmanifest->init_phase->settings->rwmsg->multi_broker
      && rwvx->rwvcs->pb_rwmanifest->init_phase->settings->rwmsg->multi_broker->has_enable
      && rwvx->rwvcs->pb_rwmanifest->init_phase->settings->rwmsg->multi_broker->enable) {
    broker_instance_id = vm_instance_id ? vm_instance_id : 1;
  }

  info->rwmsg_endpoint = rwmsg_endpoint_create(
    1,
    instance_id,
    broker_instance_id,
    info->rwsched_instance,
    info->rwsched_tasklet_info,
    info->rwtrace_instance,
    rwvx->rwvcs->pb_rwmanifest->init_phase->settings->rwmsg);

  rwtasklet_info_ref(info);
  free(instance_name);

  return info;

err:
  if (info)
    free(info);

  if (instance_name)
    free(instance_name);

  return NULL;
}
Beispiel #4
0
rwsched_instance_t *
rwsched_instance_new(void)
{
  struct rwsched_instance_s *instance;

  // Allocate memory for the new instance

  // Register the rwsched instance types
  RW_CF_TYPE_REGISTER(rwsched_instance_ptr_t);
  RW_CF_TYPE_REGISTER(rwsched_tasklet_ptr_t);
  rwsched_CFRunLoopInit();
  rwsched_CFSocketInit();

  // Allocate the Master Resource-Tracking Handle
  g_rwresource_track_handle = RW_RESOURCE_TRACK_CREATE_CONTEXT("The Master Context");

  // Allocate a rwsched instance type and track it
  instance = RW_CF_TYPE_MALLOC0(sizeof(*instance), rwsched_instance_ptr_t);
  RW_CF_TYPE_VALIDATE(instance, rwsched_instance_ptr_t);

  // Set the instance configuration
  instance->config.single_thread = TRUE;

  // For now use libdispatch only
  instance->use_libdispatch_only = TRUE;
  // libdispatch_init();

  // Fake up a rwqueue placeholder object to use as the (NULL) DISPATCH_TARGET_QUEUE_DEFAULT
  RW_ASSERT(instance->use_libdispatch_only);
  instance->default_rwqueue = (rwsched_dispatch_queue_t) RW_MALLOC0_TYPE(sizeof(*instance->default_rwqueue), rwsched_dispatch_queue_t);
  RW_ASSERT_TYPE(instance->default_rwqueue, rwsched_dispatch_queue_t);
  instance->default_rwqueue->header.libdispatch_object._dq = DISPATCH_TARGET_QUEUE_DEFAULT;

  // Fake up a rwqueue placeholder object to use as DISPATCH_TARGET_QUEUE_MAIN
  RW_ASSERT(instance->use_libdispatch_only);
  instance->main_rwqueue = (rwsched_dispatch_queue_t) RW_MALLOC0_TYPE(sizeof(*instance->main_rwqueue), rwsched_dispatch_queue_t);
  RW_ASSERT_TYPE(instance->main_rwqueue, rwsched_dispatch_queue_t);
  instance->main_rwqueue->header.libdispatch_object._dq = dispatch_get_main_queue();

  // Fake up rwqueue placeholder objects for the usual four global
  // queues.  The pri values are not 0,1,2,3 or similar, they are
  // -MAX, -2, 0, 2.  We do not support arbitrary pri values, although
  // I think the dispatch API is intended to.
  RW_ASSERT(instance->use_libdispatch_only);
  RW_STATIC_ASSERT(RWSCHED_DISPATCH_QUEUE_GLOBAL_CT == 4);
  static long pris[RWSCHED_DISPATCH_QUEUE_GLOBAL_CT] = {
    DISPATCH_QUEUE_PRIORITY_HIGH,
    DISPATCH_QUEUE_PRIORITY_DEFAULT,
    DISPATCH_QUEUE_PRIORITY_LOW,
    DISPATCH_QUEUE_PRIORITY_BACKGROUND
  };
  int  i; for (i=0; i<RWSCHED_DISPATCH_QUEUE_GLOBAL_CT; i++) {
    instance->global_rwqueue[i].pri = pris[i];
    instance->global_rwqueue[i].rwq = (rwsched_dispatch_queue_t) RW_MALLOC0_TYPE(sizeof(*instance->global_rwqueue[i].rwq), rwsched_dispatch_queue_t);
    RW_ASSERT_TYPE(instance->global_rwqueue[i].rwq, rwsched_dispatch_queue_t);
    instance->global_rwqueue[i].rwq->header.libdispatch_object._dq = dispatch_get_global_queue(pris[i], 0);
    RW_ASSERT(instance->global_rwqueue[i].rwq->header.libdispatch_object._dq);
  }

  instance->main_cfrunloop_mode = kCFRunLoopDefaultMode;
  //instance->main_cfrunloop_mode = CFSTR("TimerMode");
  //instance->deferred_cfrunloop_mode = CFSTR("Deferred Mode");

  // Allocate an array of tasklet pointers and track it
  rwsched_tasklet_t *tasklet = NULL;
  instance->tasklet_array = g_array_sized_new(TRUE, TRUE, sizeof(void *), 256);
  g_array_append_val(instance->tasklet_array, tasklet);

  rwsched_instance_ref(instance);
  ck_pr_inc_32(&g_rwsched_instance_count);
  //RW_ASSERT(g_rwsched_instance_count <= 2);
  g_rwsched_instance = instance;

  instance->rwlog_instance = rwlog_init("RW.Sched");

  // Return the instance pointer
  return instance;
}